select table, check and prompt when value match - javascript

I have my table named sup_mon:
genmat_id mat_name Stock_balance
1 bar 50
5 steel 10
20 bolt 5
HTML:
<?php
$resource2=mysql_query("SELECT * FROM genmaterial WHERE mat_name='$mat' AND size='$unit' ORDER BY genmat_id DESC limit 0,1",$con);
while($rows2=mysql_fetch_array($resource2))
{
?>
<form name="stockout" method="post" action="stock_out.php">
<label><b>Material</b></label>
<input maxlength="25" type="text" readonly name="material" id="material" required="required" style="height:20px" value="<?php echo $rows['mat_name']; ?>" >
<input type="number" name="stock_out"/>
<input type="submit" name="submit" value="submit"/>
</form>
<?php };?>
How can I check first stock_balance from the sup_mon table for the material in the material text box from the html? and prompt reorder message when stock_balance of that particular material is less than 10?

You would need to add a second query inside the loop to check if stock_balance is less than 10 like this:
<?php
$resource2=mysql_query("SELECT * FROM genmaterial WHERE mat_name='$mat' AND size='$unit' ORDER BY genmat_id DESC limit 0,1",$con);
while($rows2=mysql_fetch_array($resource2))
{
$resource3=mysql_query("SELECT * FROM sup_mon WHERE genmat_id = '" . $rows2['genmat_id'] . "'",$con);
$row3 = mysql_fetch_array($resource3);
if($row3['Stock_balance'] < 10)
{
echo "Stock balance is less then 10 please reorder";
header("refresh: 5; order.php");
}
?>
<form name="stockout" method="post" action="stock_out.php">
<label><b>Material</b></label>
<input maxlength="25" type="text" readonly name="material" id="material" required="required" style="height:20px" value="<?php echo $rows['mat_name']; ?>" >
<input type="number" name="stock_out"/>
<input type="submit" name="submit" value="submit"/>
</form>
<?php }; ?>

Related

Get the values of while loop using button with js

How can I get the values in the while loop using button with js?
<?php
while ($row = mysqli_fetch_array($query))
{
echo '<input type="text" name="sample" value="'.$row['sample'].'">';
}
?>
Thanks
You can get the value of textbox when you click on a button.
<input type="text" name="sample[]" value="abc" class="valueInput">
<input type="text" name="sample[]" value="xyz" class="valueInput">
<input type="text" name="sample[]" value="pqr" class="valueInput">
<input type="button" class="getValue" value="Get Value">
Note: I have set the static input box you can make it dynamic but make sure please add the class as valueInput.
Jquery Code.
$(document).on('click','.getValue',function(){
var valArr = [];
$(".valueInput").each(function(){
valArr.push($(this).val());
});
console.log(valArr);
})
The name of the input field should indicate an array with [].
<form action="action.php" method="POST">
<?php
while ($row = mysqli_fetch_array($query)){
echo '<input type="text" name="sample[]" value="'.$row['sample'].'">';
}
?><input type="submit">
</form>
action.php: (Processes the data on submit)
<?php
echo "<pre>";
print_r($_POST['sample']);
echo "</pre>";

How to fill textboxes based from combobox

I'm still learning Web programming and I'm having a problem on how I'm going to fill my 3 textBoxes based from chosen option in combobox.
I tried using php inside javascript but it seems I'm not doing it right. What I'm trying to do is to load the name, age and address of of a user based from the chosen option combobox so that I can update their data.
<?php
include("myconnection.php");
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<script>
function listUpdate()
{
var ddl=document.getElementById("userlist");
var selectedOption=ddl.options[ddl.selectedIndex];
var nameNya=selectedOption.getAttribute("value");
console.log(nameNya);
var tb1=document.getElementById("nameid");
var tb2=document.getElementById("ageid");
var tb3=document.getElementById("addressid");
tb1.value="gago";
<?php
/*
$sql="SELECT * from users WHERE fullname='nameNya'";
mysqli_query($db,$sql);
$nameNyaa=$_POST['nameNya'];
echo $nameNyaa;*/
?>
}
</script>
</head>
<body>
<form action="updateuserprocess.php" method="POST">
<h1>UPDATE USER</h1>
<?php
$sql="SELECT * from users";
$result=mysqli_query($db,$sql);
echo '<select id="userlist" onChange="return listUpdate()">';
while($row=mysqli_fetch_array($result))
{
echo "<option value='".$row[3]."'>".$row[3]."</option>";
}
echo '</select>';
?>
<br>
<label>Name: </label>
<input type="text" name="name" placeholder="" id="nameid"><br>
<label>Age: </label>
<input type="text" name="age" placeholder="" id="ageid"/><br>
<label>Address: </label>
<input type="text" name="address" placeholder="" id="addressid"/><br>
<br>
<input type="submit" name="submit" value="OK"/>
<button type="submit" formaction="/myhome.php">Back</button>
</form>
</body>
</html>
<?php
if(isset($_POST["submit"]))
{
include("updateuserprocess.php");
}
?>
Try doing it like this
$('#mycomboboxID').change(function() {
var combobox_value = $('#mycomboboxID').val();
$('#myInput1').val('First');
$('#myInput2').val('Second');
$('#myInput3').val('Third');
}
You can do if statement inside as well,
and remember you need a jquery extension for this to work

Entered Fields should not be cleared with alert box is displayed in php

I am trying to display a message if the amount entered greater than 5000 alert box or echo messsage "Enter a pan card no" as to be displayed when I click on submit button. In my code Message gets displayed but all the fields which data has been already entered gets cleared. I dont want the data which is entered to get cleared.
Here is the code.
<?php
include_once "db.php";
if(isset($_POST['submit'])) {
$amount = mysql_real_escape_string($_POST["amount"]);
$firstname=mysql_real_escape_string($_POST["firstname"]);
$phone=mysql_real_escape_string($_POST["phone"]);
$address=mysql_real_escape_string(stripslashes($_POST["address"]));
$pan=mysql_real_escape_string(stripslashes($_POST["pan"]));
$query= mysql_query("INSERT INTO payment (amount,selector1,firstname,lastname,email,phone,address,country,state,pan)VALUES('$amount','$selector1','$firstname','$lastname','$email','$phone','$address','$country','$state','$pan') ") or die(mysql_error());
if($amount>="5000")
{
echo "Enter Pan Card No";
}
else {
$id=mysql_insert_id();
$_SESSION['sess_user'] = $id;
$_SESSION['lastname'] = $lastname;
$_SESSION['address']=$address;
header("Location:successreg.php");
}
}
?>
<form action="" method="post" name="payment">
<div class="w3l-user">
<input type="text" name="amount" placeholder="₹ My Contribution" required="">
</div>
<input type="text" name="firstname" placeholder="Firstname" required="">
<input type="text" name="pan" placeholder="Pan Card No" >
</div>
<div class="btn">
<input type="submit" name="submit" value="REGISTER"/>
</div>
</div>
<div class="clear"></div>
</form>
</div>
</div>
In order to show values, after for submission you should get values from $_POST and use them in input values:
$firstname = '';
if(isset($_POST['submit'])) {
// write below line after `if`
$firstname = $_POST['firstname'];
Now, change your HTML to:
<input type="text" name="firstname" placeholder="Firstname" required="" value="<?php echo $firstname; ?>">
For Radio buttons:
$firstRadio = $secondRadio = '';
if(isset($_POST['submit'])) {
// if first radio is selected which you will know from $_POST['firstRadio']
$firstRadio = 'selected';
And in HTML:
<input type='radio' <?php echo $firstRadio;?> value=1 name='firstRadio' />

not insert into particular field and parrticular name

i want to insert picture in particular name but there is no insert and some facing error
my table like this
how can insert pic in front of particular name
config.php file is
<?php
session_start();
$db = new mysqli('localhost','root','','dharmesh');
?>
and code is here :
<?php
include_once('config.php');
if(isset($_POST['family_member_btn'])) {
$family_member = $_POST['family_member'];
}
if(isset($_POST["submit4"])) {
for($i=0;$i<$_POST['num'];$i++) {
$insert = $db->query("INSERT into `multiple_insert` (`f_name`,`m_name`,`l_name`,`birth_date`) values ('".$_POST['f_name'][$i]."','".$_POST['m_name'][$i]."','".$_POST['l_name'][$i]."','".$_POST['b_date'][$i]."')");
if($insert) {
echo "<script>alert('insert successfully');</script>";
} else {
echo "<script>alert('!!!insert unsuccessfully');</script>";
}
}
}
if(isset($_POST["upload"])) {
for($i=0; $i<$_POST['img'];$i++) {
$imag = $_FILES['f_name_pic']['name'][$i];
$tmp = $_FILES['f_name_pic']['tmp_name'][$i];
$dir = "images/".$imag;
move_uploaded_file($tmp,$dir);
$query = $db->query("UPDATE `multiple_insert` SET `picture`='$dir' WHERE f_name='".$_POST['f_name'][$i]."' where user_id='1'");
}
}
?>
<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="">
<?php
$select = $db->query("SELECT * from multiple_insert where user_id='1'");
while($select_f_name = $select->fetch_assoc()){
echo "<input type='hidden' value='1' name='img' />";
echo "<p style='background-color:red;color:yellow;width:5%;'>".$select_f_name['f_name']."</p>";
echo "<span><input type='file' name='f_name_pic[]' /></span><br><br>";
}
echo "<button name='upload'>Uplaod</button><br><br>";
?>
<label><font size="2">HOW MANY MEMBER IN YOUR FAMILY ?</font></label><br><br>
<input type="text" name="family_member" class="form-control" />
<button type="submit" name="family_member_btn" class="btn btn-lg btn-info" /><span>SUBMIT</span></button><br><br>
<?php
for($i=1;$i<=$family_member;$i++) {
?>
<input type="hidden" value="<?php echo $family_member;?>" name="num" />
<label><b><?php echo "RECORED # ".$i;?></b></label><br>
<label><font size="2">First Name</font></label>
<input type="text" name="f_name[]" class="form-control" /><br>
<label><font size="2">Middle Name</font></label>
<input type="text" name="m_name[]" class="form-control" ><br>
<label><font size="2">Last name</font></label>
<input type="text" name="l_name[]" class="form-control" /><br>
<label><font size="2">birthdate</font></label>
<input id="datepicker3" class="form-control" name="b_date[]" type="text" />
<hr style="border:solid 2px rgba(0,0,0,0.2);">
<?php } ?>
<button class="btn btn-info btn-cons from-left pull-right" type="submit" name="submit4">
<span>SUBMIT</span>
</button>
</form>
</body>
</html>
how can i done this please send me updated code for this...
You cannot do that! The interface you are looking at, is phpMyAdmin---unless you are willing to hack the core code (HIGHLY NOT RECOMMENDED TO MODIFY CORE CODE OF ANY PROJECT!).

Failure to display result of a php script calculation in the textarea box

I believe that my problem may lie in the way that I am trying to initialize the script. The submit button that I am using seems to just clear the form, and not display any results. I also think there may be a problem with the two lines " $message = $_POST['textarea'];" and "echo $result + $message;" I am not sure if that is the correct way to produce a result into a text area box, as well as display the miles driven and total cost in their separate text boxes. I want this one button to do all three tasks. Is there a way to do this efficiently with PHP?
Edit: Corrected version of the code. Thank you #Fred-ii-!
<?php
if(isset($_POST['submit'])) {
$x = $_POST['beginningOdometerReading'];
$y = $_POST['endingOdometerReading'];
$z = $_POST['daysRentedCar'];
$miles = $y - $x;
$result = (15 * $z) + ($miles * 0.12);
$message = $_POST['textarea'];
echo $result + $message; }
?>
<html>
<head>
</script>
</head>
<body>
<div align="center">
<hr><br>
<form action="index4.php" method="post" name id="Main">
<input type="text" id="name" name="customerName" placeholder="Enter your name here" size="30px">
<br><br>
<input type="text" id="address" name="customerAddress" placeholder="Enter your street address here" size="50px">
<br><br>
<input type="text" id="city" name="customerCity" placeholder="What city do you live in?" size="30px">
<br><br>
<input type="number" id="zip" name="customerZip" placeholder="Enter your zip code" size="30px">
<br><br>
<input type="number" id="bOdometer" name="beginningOdometerReading" placeholder="Start odometer reading" size="80px">
<br><br>
<input type="number" id="eOdometer" name="endingOdometerReading" placeholder="End odometer reading" width="80px">
<br><br>
<input type="number" id="daysRented" name="daysRentedCar" placeholder="Days rented" size="50px">
<br><br>
<input type="submit" id="submit" value="Submit"/>
<br><br>
Miles Driven: <input type="number" id='miles' min="1" max"10000" readonly="" />
Total Cost: <input type="number" id='result' min="1" max"10000" readonly="" />
<br><br>
Summary: <textarea cols="30" rows="2" name="thetextarea" id="textarea"> </textarea>
<br><br>
<input type="reset" value="Reset">
</form>
<hr>
</div>
</body>
There are a few things wrong here.
One of the problems being with your conditional statement:
if(isset($_POST['submit'])) {
it is looking for a named element called "submit" which your submit button is not named.
<input type="submit" id="submit" value="Submit"/>
name it:
<input type="submit" name="submit" id="submit" value="Submit"/>
and you might have been relying on "id" for it. In this instance, you can't.
That is why you're getting back a blank page.
Having used an else{ echo "Error"; } would have echo'd "Error".
For example:
<?php
if(isset($_POST['submit'])) {
$x = $_POST['beginningOdometerReading'];
$y = $_POST['endingOdometerReading'];
$z = $_POST['daysRentedCar'];
$miles = $y - $x;
$result = (15 * $z) + ($miles * 0.12);
$message = $_POST['textarea'];
echo $result + $message; }
else { echo "Error"; }
?>
Then you have
$message = $_POST['textarea'];
but your textarea is named name="thetextarea".
Both of those need to match in names.
You are also using the wrong concatenate operator + for
echo $result + $message;
that needs to be a dot. The + sign is a JS/C++ method of concatenation, if one of those variables contains a string.
echo $result . $message;
or
echo $result . " ". $message;
Had it been a mathematical equation from two variables, then yes; that would have been a valid operator, just not in this case, since you are trying to echo from the $message variable, which is for the "textarea", being "text" and not an integer.
Sidenote: To ensure that you are indeed passing an integer to a POST array, add (int) to it. Example: $x = (int)$_POST['beginningOdometerReading'];
Edit: I found a few more errors in your form.
You have 2x instances of max"10000" it's missing an equal sign max="10000"
and you might be missing name attributes for both of these (which I already outlined are missing = signs for max"10000".
Miles Driven: <input type="number" id='miles' min="1" max"10000" readonly="" />
Total Cost: <input type="number" id='result' min="1" max"10000" readonly="" />
If you're relying on JS as you tagged this as, please edit your question and add that code. However, there was no code in your PHP to support this.
You've a syntax error in <form> being name id="Main"
You will need to adjust your actual code accordingly.
Rewrite:
<?php
if(isset($_POST['submit'])) {
$x = $_POST['beginningOdometerReading'];
$y = $_POST['endingOdometerReading'];
$z = $_POST['daysRentedCar'];
$miles = $y - $x;
$result = (15 * $z) + ($miles * 0.12);
$message = $_POST['thetextarea'];
echo $result . " " . $message; }
else { echo "Error"; }
?>
Error reporting would have signaled an undefined index notice for both of those.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Try to give name for input type `submit` button like
<input type="submit" name="submit" id="submit" value="Submit"/>
And try to remove from action='' because when you submit it directly takes to the index4.php and
error in form statement also you does not provide name you leave it empty like name id="Main"
<form action="index4.php" method="post" name="Main" id="Main">
And use echo statement like
echo $result,$message; }

Categories