This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>
Related
i am new to php and while i am practing i came across a problem. actually,i have two files index1.php and index2.php. in index1.php i have a link with a unique id as
<a href="index2.php?companyid=<?php echo $row('company_id');?>>details</a>
i have got this value in index2.php as
if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
now i have a search form in the index2.php as
<form method="POST" action="index2.php">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
now on button click i want the search results be displayed in the same page as
'index2.php?companyid=$companyid'
but some how if i try to use $_POST['submit']; in the same page it takes me to index2.php and instead of index2.php?companyid=$companyid and also it throws error as undefined index of $companyid if i don't use $_POST['submit']; and echo $companyid; it gives value and works fine. all i want is that to use $companyid' value inside ``$_POST['submit']; as and display the result in the same url as before
if(isset($_POST['submit']){
$companyid //throws an error index of company id
}
any help will be appreciated
First off, it looks like you are not using the company id in the form itself, so it will not be submitted as part of the the POST. You could possibly use:
<form method="POST" action="index2.php">
<?php if (isset($companyid)): ?>
<input type="hidden" name="companyid" value="<?= $companyid; ?>">
<?php endif; ?>
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
But you would probably also need to change your logic to:
if(isset($_POST['companyid'])){
$companyid = $_POST['companyid'];
}else if(isset($_GET['companyid'])){
$companyid = $_GET['companyid'];
}
As Josh pointed out in the comments, PHP is not able to remember your previous GET request but this can easily be solved by altering the action attribute of the form element. By doing this you can pass on the previous data. This would look a little something like this:
<form method="POST" action="index2.php?companyid=<?php echo $companyid;?>">
<input type="text" name="search">
<button type="submit" name="submit">submit</button>
</form>
This way you will be redirected to index2.php with the URL parameters present and you will be able to retrieve both search and companyid using $_POST and $_GET or use $_REQUEST for both.
I have a form with few input fields and a update option ,suppose if i have 10 fields and i update only first two fields and not the rest who can i display a message that only the first to field A and B are updated using PHP and Mysqli
in the screenshot if i update the value of bill id and origin i should display message out in some other page that says that bill id and origin has been updated with "xyz " value
One way is to fetch fields with real and old name i.e.
Select name,name as old_name from table
In html
<input type="text" name="name" value="name from db">
<input type="hidden" name="old_name" value="old_name from db">
After submit form you can check it by.
And check after post.
If(post['name']==post['old_name'])
Something like this.
Another way is from js by checking onchange event
well it'll be something like this ..
html:
<html>
<body>
//action is defining the php file's name which the form will be sent to ;
//method is defining the method which the form will be sent with
<form action="updateCheck.php" method="post">
<input type="text" name="input-filed-1"/>
<input type="text" name="input-filed-2"/>
<input type="text" name="input-filed-3"/>
<input type="text" name="input-filed-4"/>
<input type"text" name="input-filed-5"/>
<input type="submit" value="submit/>
</form>
</body>
</html>
create a file named updateCheck.php in the same directory and enter the following code
<?php $updateMessage = "you have updated the following fields";
for($x = 0 ; $x<=5 ; $x++)
{
// trim for erasing the whitespaces
// that if clause checks if there was data entered in the update input fields
if(isset($_POST["input-field-".$x]) && trim($_POST["input-field-".$x]))
{
$updateMessage.= "input-field-".$x ;
}
}
echo "<script type='text/javascript'>alert('".$updateMessage."')</script>"
?>
I haven't tested the code .. but study it carefully and you'll see how it's done
I want to create a checkbox and check whether checkbox is checked or not in another PHP page so I created form like :
<form action="heartbeat.php" method="post">
<input type="checkbox" name="keepme">
<input type="submit" value="log in">
</form>
and then I tried to extract this input on heartbeat.php like:
<?php
/*
* A PHP file for laying down a heartbeat JavaScript call.
*/
if(!isset($_POST["keepme"])){
$auto_logout = 10;
}
else{
$auto_logout = 1000;
}
?>
but it never gets $_POST["keepme"] value. Any idea??
If you don't pass value to checkbox tag, the POST array will be empty. You need to do something like that
<input type="checkbox" name="keepme" value='1'>
Make Sure you have submit button
If(isset($_POST['submit-btn'])){
echo $_POST['keepme'];
}
When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?
I have spent quite some time making a function and the last 15-20 minutes trying to figure this out. I need help!
I am selecting multiple rows from the database and then running them in a while loop.
They are available on a dropdown menu.
<form method="POST" action="adminprocess.php">
<fieldset>
<p>
<label class="left2">League:</label>
<select name="league" class="combo">
<?php
$q = $database->selectAllLeagues();
while($row=mysql_fetch_assoc($q))
{
$theid = $row['id'];
extract($row);
?>
<option value="<? echo $theid; ?>">
<? echo $format.'_'.$game.'_'.$name.'_Season '.$season;?>
</option>
<?
}
?>
</select>
</p>
<p>
<input type="hidden" name="replaceleague" />
<input type="hidden" name="format" value="<? echo $format; ?>" />
<input type="hidden" name="game" value="<? echo $game; ?>" />
<input type="hidden" name="name" value="<? echo $name; ?>" />
<input type="hidden" name="season" value="<? echo $season; ?>" />
<input type='submit' class="button" value='Select league' />
</p>
</fieldset>
</form>
$theid seems to be working fine dependning on which row i select on the dropdown menu.
However, I cant get the values below in the hidden inputs to pass through the correct values from the row selected in the dropdown box.
It seems to always pass through the 4 variables from the first row of the database.
So basically, I need it to select the right row and use that data.
What am i doing wrong!!!
Thanks for reading!
Your hidden fields are initialized outside the loop, so they'll use the values that were left over from the last iteration of the while loop. (i.e. the last fetched row)
Why do you actually need the hidden fields in the first place? When you submit the form, the league field will contain the ID of the row selected in the drop-down box. Using the ID, you can fetch the other fields from the database when processing the form.
To directly answer your question about the while loop, it's because the hidden inputs are echoed outside the loop, after which data the last-iterated row from your database is used by PHP to output to those hidden inputs.
But I suggest that instead of using hidden form elements like that, you submit your form with the <option> with the value a user chooses, read the value (as in $_POST['league']), and fetch the row from your database with that ID and use it accordingly. (You may wish to keep the replaceleague hidden input if your application needs it of course.)
It's much easier, plus it ensures the information about the row a user chooses is coming from your database and not tampered with. In fact, for most applications this is the right way to go.