Passing input values using post - php

I am trying to pass the value of <input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>"> to the exchange.php page. Lets say i have apple | 1 , orange | 2, pineapple | 3. However, whenever i try to submit the values to another page, I am getting the 3 as the echo value of $product. When i try to remove the type="hidden", I get the correct value but when I try to submit, it turns out to be a different value.
<form action="exchange.php" method="post">
<div class="row">
<?php $query="SELECT * FROM Product" ; $data=$ MySQLi_CON->query($query);
foreach ($data as $key ) { ?>
<strong>Name: </strong>
<?php echo $productname=$ key[ 'product_Name'];?>
<input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>">
<strong>Status: </strong>
<strong>Action: </strong>
<input type="submit" value="Exchange" name="exchange_submit" class="btn btn-info btn-xs">
<input id="id" name="id" type="hidden" value="<?php echo $id; ?>">
<?php } ?>
</div>
</form>
exchange.php
<?php
$id = $_POST['id'];
$product = $_POST['ids'];
echo $id;
echo $product;
exit;
?>

<?php echo $productname=$ key[ 'product_Name'];?>
//is it working not showing error

Whenever you execute a foreach loop, it will store the last output of the Database table in the hidden input of yours, and that's why no matter any input you give, it will take 3 as the id of the product.Try using a tag also.

You are using foreach for products so I consider you've more than one products.
but when you doing this you are overwriting the $_POST['ids'].
<input id="ids" type="hidden" name="ids" value="<?php echo $key['product_ID'];?>">
thats why its showing last product_ID present in table.

Related

how to select multiple rows in table via checkbox and pass these values to another table

I want to select multiple rows in a table via checkboxes and store these values in another table.I have almost done. But problem is that when I select multiple rows and get these values via array. It only fetch single value. I want all selected values. Please help me in this regard. My code is given below.
<form class="form-horizontal" method="post" action="add_exam_quiz.php" role="form" id="form1">
<tr>
<td>
<input type="checkbox" name="quiz_question[<?php echo $question_no; ?>]" value="<?php echo $question_no; ?>">
<?php echo $question_no; ?>
</td>
</form>
</tr>
<input class="btn btn-primary" type="submit" name="submit" value="submit" form="form1">
I expect an array with all checked values. But I got single value.
You need to have all the checkboxes with the same name like this:
<td><input type="checkbox" name="quiz_question[]" value="<?php echo $question_no; ?>"> <?php echo $question_no; ?> </td>
then in PHP:
<?php
$quiz_question = $_POST['quiz_question'];
foreach ($quiz_question as $question=>$value) {
echo $value."<br />";
}
?>
Try to give same NAME for all check box options. If you give different NAME it won't work.

Three page sequence of forms (checkboxes and text/number boxes) with foreach in php

I would like to pass variables through 3 pages. The 1st page asks the user which music genres they like (there will eventually be 20+ genres). The 2nd page asks the user to rank the genres they have selected and the 3rd page sorts and displays their ranking.
This first page asked the user to pick which genres they like:
<form id="genre" name="genre" method="post" action="musicsell.php">
<input type="checkbox" name="genre[]" value="Rap"/>Rap<br />
<input type="checkbox" name="genre[]" value="HipHop"/>HipHop<br />
<input type="checkbox" name="genre[]" value="RnB"/>RnB<br />
<input type="checkbox" name="genre[]" value="Rock"/>Rock<br />
<input type="checkbox" name="genre[]" value="Jazz"/>Jazz<br />
<p>
<input type="submit" value="Next">
<br />
</p>
</form>
This second asks them to rank (prioritize) the genres they have selected with 1 being the best:
<body>
The genre(s) you selected are: <br>
<form id="form1" name="form1" method="post" action="musicresults.php">
<?php
$name = $_POST['genre'];
if(isset($_POST['genre'])) {
foreach ($name as $genre){
?>
<input
type="number" required="required" id="<?php echo $genre ?>" name="music[<?php echo $genre ?>]" />
<?php echo $genre ?><br />
<?php
}
}
?>
<input type="submit" name="button" id="button" value="Submit" /></form>
</body>
The third and last page sorts and echos the results:
<?php
//Get the form results (which has been converted to an associative array) from the $_POST super global
$musicgenres = $_POST['music'];
//Sort the values by rank and keep the key associations.
asort($musicgenres, SORT_NUMERIC );
//Loop over the array in rank order to print out the values.
foreach($musicgenres as $music => $rank)
{
echo "$musicgenres is your $rank choice";
echo "<br>";
}
?>
It all works fine until the last page where I'm getting an "array to string conversion" error. Maybe I need to put in session variables but I'm not sure.
Any help would be appreciated.
It's exactly what the error says. It can't convert and array to a string.
Replace
echo "$musicgenres is your $rank choice";
with
echo "$music is your $rank choice";

Auto-Increment Form Input Names With $_SESSION

I want to use PHP $_SESSION to access user form-inputted data. The flow looks like this:
STEP 1
User fills out form.
<form method="post" action="review.php">
<input type="text" name="length">
<input type="text" name="width">
<input typ="text" name="quantity">
<input type="submit" value="submit">
</form>
STEP 2
Form data is submitted and 'review.php' converts from POST to SESSION. User reviews form details and adds item to cart.
STEP 3
Item is added to PayPal Shopping Cart. User decides he wants to add another item so user clicks on "continue shopping" and returns to STEP 1.
STEP 1
User fills out form details. Except this time, I want the input name to auto-increment.
For example:
<input type="text" name="length2">
<input type="text" name="width2">
<input type="text" name="quantity2">
And I want it to auto increment however many number of times the user adds new items to the cart.
<input type="text" name="length3">
<input type="text" name="length4">
<input type="text" name="etc">
Usual flow continues... STEP 2 ---> STEP 3
After successful payment in STEP 3, user will be redirected to a new page.
STEP 4(newPage)
All of the purchased items' details will be displayed on this page in order and with the same group. For example, (length, width, quantity) then (length2, width2, quantity2) and so on and so forth. They will each be displayed in different forms.
Those forms will also include <input type="file"> so that the user can upload a file for each form. Each form represents a purchased item.
Once the user uploads files to each corresponding form, they will be submitted to a database.
QUESTION
How can I make the input names auto increment preferably with PHP,
and of course whilst using $_SESSION? I would like the name to
return to its original name after the user's session. So let's say
that same user wanted to order the next day and he ordered 5 items,
the input names would span to 5 (length, length2,..., length5).
How can I display all of the user inputted data in each respective
form(see STEP 4)?
I know I have to do all of the back-end stuff on PHP($_SESSION, forms, etc) but if someone has a jQuery solution for the auto increment and showing all of the data in the end I'm all for it.
Here's What I Was Working With
Tried to use this to display the data with the incremented names but couldn't figure out a solution.
<?php
session_start();
?>
<form>
<?php
$size = $_SESSION['size'];
$variables = array("$size");
$i = 1;
foreach ($variables as $var ) {
$name = "txt".$i;
echo "<input type='text' name='".$name."' value='".$var."' />";
$i++;
}
?>
</form>
Some of you might bring up PayPal IPN. I created an IPN listener as well but it doesn't get called via sandbox. A user on here posted that his IPN listener didn't work on sandbox either but when he went live via paypal.com it did work. Could be the case with mine too, but I want to go ahead and start selling so I need a different approach. Even if my IPN listener does work via paypal.com, I can't risk going live with it without being 100% sure. So if I can use this method for now until I can confirm that my listener works I will do it.
As always I am very grateful for your guys' help.
I actually figured this out a while back. Here's the code I am using:
<?php
if(isset($_POST['submit'])){
$_SESSION['invoice'] = $_POST['invoice'];
$invoice_no = $_SESSION['invoice'];
$decal = array(
'length' => $_POST['os0'],
'width' => $_POST['os1'],
'color' => $_POST['os2'],
'quantity' => $_POST['os3'],
'price' => $_POST['price'],
'submit' => $_POST['submit']
);
$_SESSION['order'][] = $decal;
$i = 0;
}
if(isset($_SESSION['order'])){
foreach($_SESSION['order'] as $sav) {
$i++;
?>
<input type="hidden" name="on0_<?php echo $i; ?>" value="Length">
<div class="decalName">
<label>Custom Decal <?php echo $i; ?></label>
</div>
<div class="label-input">
<label>Length</label><input type="text" name="os0_<?php echo $i; ?>" id="length" size="2" class="num" value="<?php echo $sav['length']; ?>" readonly>
</div>
<input type="hidden" name="on1_<?php echo $i; ?>" value="Width">
<div class="label-input"><label>Width</label><input type="text" name="os1_<?php echo $i; ?>" id="width" size="2" class="num" value="<?php echo $sav['width']; ?>" readonly>
</div>
<input type="hidden" name="on2_<?php echo $i; ?>" value="Color">
<div class="label-input">
<label>Color</label><input type="text" name="os2_<?php echo $i; ?>" value="<?php echo $sav['color']; ?>" readonly>
</div>
<div class="label-input">
<label>Quantity</label><input type="text" name="os3_<?php echo $i; ?>" size="2" class="num" id="quantity" value="<?php echo $sav['quantity']; ?>" readonly>
</div>
<input type="hidden" name="on3_<?php echo $i; ?>" value="quantity">
<input type="hidden" class="num" value="0.20">
<div class="label-input">
<label>Price $:</label><input type="text" class="tot" name="price" value="<?php echo $sav['price']; ?>" readonly>
</div><!--end label-input-->
<?php
}
?>

Change hidden field value on Submit click

I have a form with multiple items and an id attributed to each of these items, on Submit I want to be able to grab the id of the item that was clicked - I have tried using js, something like:
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>" type="submit" value="Add">
</form>
I want to be able to grab this value: $item_id = $_POST["item_id"]; on add_item_cart.php, but this doesn't seem to be working.
Is this a problem with my js syntax or is my logic not plausible to solve this problem? Is it submitting before changing the value?
EDIT:
Let's see if I can explain myself better, I want to assign that hidden value dynamically, imagine that my form has 3 submit buttons (one for each item displayed). Depending on the one that is clicked, I want to pass the item's id to my hidden field, so if I click button1 - $_POST["item_id"]=1, button2 - $_POST["item_id"]=2... etc
Here is my actual form (non simplified example)
<form method="post" action="add_item_cart.php">
<table style="width:600px">
<tr>
<?php foreach ($items as $item): ?>
<td>
<center>
<span style="font-size:20px"><?php echo $item["item_name"] ?></span><br/>
€<?php echo $item["price"] ?><br/>
Quantidade: <input type="text" value="1" style="width:30px"><br/>
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo $item["id"]; ?>" type="submit" value="Adicionar">
</center>
</td>
<?php endforeach; ?>
</tr>
</table>
</form>
When your form is posted and you want to collect the item_id value on add_item_cart.php first you need to actually assign a value to id as in (Assuming item_id is a php variable). The id is just used for setting the css editing not a value...
<input type="hidden" id="item_id" value="<?php echo $item_id; ?>" name="item_id">
you cannot have id='' for the value because 'value' is value.
Then you can get that value on your other page with:
<?php
if(isset($_POST['item_id'])){
$item_id = $_POST['item_id'];
}
?>
If you want to edit the variable after post depending on which button you hit you can try.
<input name="submit_item1" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item2" id="btn_sub" name="button1" type="submit" value="Add">
<input name="submit_item3" id="btn_sub" name="button1" type="submit" value="Add">
Then on the top of your page you can do.
<?php
if(isset($_POST['submit_item1'])){
$item_id = 1;
}
if(isset($_POST['submit_item2'])){
$item_id = 2;
}
if(isset($_POST['submit_item3'])){
$item_id = 3;
}
?>
If you are creating forms from an array of items you could do something like, (assuming you somehow have an id associated with those items; I would need to know more information about how you are making your item list. But it would generally do like this.
<?php
foreach($item_array as $item){
?>
<form method="post" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id" value="<?php echo $item['id']; ?>">
<input name="submit_item" id="btn_sub" type="submit" value="Add">
</form>
<?php
}
?>
Then on the top of your page you can just get $_POST['item_id'] and since that value is dynamically set you do not need any conditionals you can just get that value and run any query.
UPDATE
Use normal button instead of submit button and use javascript for submitting the form.
<form method="post" name="f1" action="add_item_cart.php">
<input type="hidden" id="item_id" name="item_id">
<input name="submit_item" id="btn_sub" onclick="document.getElementById('item_id').value = <?php echo '3'; ?>; document.f1.submit();" type="button" value="Add">

how to send value to the from action page from database

I am creating a faq panel for there can be multiple answers for question and i want to take the answer id .because i am storing comment by answer id
the problem is that how to sent the $answer_id to the comment_submit_process.php and how to recognize the answer ?
$selected_ques= mysql_prep($_GET['ques']);
$query = "SELECT * FROM formanswer where question_id = {$selected_ques}";
$ans= mysql_query($query);
if($ans){
while($answer = mysql_fetch_array($ans))
//here is the form
<form id="add-comment" action="comment_submit_process.php" >
<textarea class="comment-submit-textarea" cols="78" name="comment" style="height: 64px;"></textarea>
<input type="submit" name="submitbutton" value="Add Comment" class="comment-submit-button" >
<br> <?php
$ans_id= $answer['id']; //i am fatching the $answer['id'] from database
?>
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>" />
<span class="counter ">enter at least 15 characters</span>
<span class="form-error"></span>
</form>
<?php }} ?>
You might have typo here !! it should be..
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
Other thing, you can add get param to action link it self.
<form id="add-comment" action="comment_submit_process.php?<?php echo $answer['id']; ?>" >
Instead of setting the ans_id, every time to the hidden field .
Generate a string of ans_id seperated with "," until while loop ends append the string and assign that value to the hidden field and in form action page you can get that value and generate van array from that string with delimiter ",".Now you can have the array of ans_id in your form action page
$answer_array = "nothing";
while($answer = mysql_fetch_array($ans))
{
if( $answer_array == "nothing")
$answer_array = $answer;
else
$answer_array .= ",".$answer;
}
<input type="hidden" name="answer_arr" value="<?=$answer_array?>">
In Form action page you can get that hidden value
$ans_array= explode(",",$_GET['answer_arr']);
You can echo answer_id in form action tag as additional parameter like this:
<form id="add-comment" action="comment_submit_process.php?ans_id=$ans_id" >
//Your stuff here
</form>
in comment_submit_process.php you can identify answer by
$ans_id=$_GET['ans_id'];
You can do further processing by using $ans_id
Edit:
change this line:
<input type="hidden" name="ques" value="<?php echo $_GET['$ans_id'] ?>"
to:
<input type="hidden" name="ques" value="<?php echo $ans_id; ?>" />
so that value of that field would be $ans_id fetched from DB.

Categories