This is my HTML:
<form method="POST" action="">
<?php
$skillSubCategory = $skills->showSkills(24);
for ($i = 0; $i < count($skillSubCategory); $i++) {
?>
<input type="hidden" name="skillid" value="<?php echo $skillSubCategory[$i]['skill_id']; ?>" />
<?php echo $skillSubCategory[$i]['title']; ?>
<input type="submit" name="add" value="add" /><br />
<?php } ?>
</form>
<?php if (isset($_POST['add'])) {
echo $_POST['skillid'];
} ?>
Resulting source code:
<form method="POST" action="">
<input type="hidden" name="skillid" value="25" />
Animal Grooming
25
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="26" />
Dog Trainer
26
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="27" />
Dog Walking
27
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="28" />
Vet
28
<input type="submit" name="add" value="add" /><br />
<input type="hidden" name="skillid" value="29" />
Beekeeping
29
<input type="submit" name="add" value="add" /><br />
</form>
What it looks like:
I get number 29 for any button clicked. Any ideas what's wrong? Why the correct number wont show up when i click add?
You can also use the buttons themselves(without changing their values):
<input type="submit" name="skillid[25]" value="add" />
<input type="submit" name="skillid[26]" value="add" />
<input type="submit" name="skillid[27]" value="add" />
To retrieve the submitted value(its not the value in this case, its the first key of the posted array):
if(isset($_POST['skillid']) && is_array($_POST['skillid']))
{
echo key($_POST['skillid'])
}
Because when you have multiple fields with the same name attribute in a form, the last one always takes precedence (with the exception of submit buttons -- the one clicked will be the only one considered). So the last hidden input with the name skillid will always be sent to the server.
When using forms like this, you usually have to use separate forms for each button. Alternatively, change the value attribute of each button and consider that from your PHP code.
Change:
<form method="POST" action="">
to:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
then change the condition to:
if (isset($_POST['add']) && isset($_POST['skillid'])) {
EDIT: use the <option> tag instead
<select name="skillid">
<option value="25">Animal Grooming</option>
<option value="26">Dog Trainer</option>
...
</select>
Your PHP code now will be:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$skillSubCategory = $skills->showSkills(24);
<select name="skillid">
for ($i = 0; $i < count($skillSubCategory); $i++) { ?>
<option value="<?php echo $skillSubCategory[$i]['skill_id']; ?>"><?php echo $skillSubCategory[$i]['title']; ?></option>
<?php } ?>
</select>
<input type="submit" name="add" value="add" /><br />
</form>
if (isset($_POST['add']) && isset($_POST['skillid'])) {
echo $_POST['skillid'];
} ?>
Related
I can display first name, last name and the instrument they borrowed. There is a drop down if they want to change the instrument they borrowed. Why is my code not changing the instrument that the person is changing? What could be wrong in this code? Please help. I know my code is not agreeable to sql securement, this is just an activity. database
<html>
<head>
</head>
<body>
<form method="post">
<?php
$con=mysqli_connect("localhost","root","","borrow");
$q="SELECT students.studentid,students.studfname,students.studlname,instruments.instrumentname, instruments.instrumentid from students INNER JOIN student_instrument ON students.studentid=student_instrument.id INNER JOIN instruments ON student_instrument.checkoutdate=instruments.dateacquired";
//SELECT students.studentid,instruments.instrumentid,students.studfname,students.studlname,instruments.instrumentname from
//students INNER JOIN instruments ON students.studentid=instruments.instrumentid
$t="SELECT instrumentname from instruments where dateacquired='avail'";
$r=mysqli_query($con,$q);
$e=mysqli_query($con,$t);
while($result=mysqli_fetch_array($r)){
$id=$result['instrumentid'];
$studfname=$result['studfname'];
$studlname=$result['studlname'];
$instrumentname=$result['instrumentname'];
echo $studfname." ".$studlname." ".$instrumentname." ";
echo '<input type="hidden" name="id" value='.$id.'>';
echo '<select name="inst">';
while($f=mysqli_fetch_array($e)){
$avail=$f['instrumentname'];
echo '<option>'.$avail.'</option>';
}
echo '</select>';
echo '<input type="submit" value="submit" name="submit"><br>';
}
?>
</form>
<?php
if(isset($_POST['submit'])){
$id=$_POST['id'];
$inst=$_POST['inst'];
$p="UPDATE instruments SET instrumentname='$inst' WHERE id='$id'";
$q=mysqli_query($con,$p);
if($q){
header('location:student.php');
}
}
?>
</body>
</html>
Check the generated HTML code and look for the <form> and </form> tag. Currently you have a form similar like this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
<input type="hidden" name="id" value="8" />
<input type="submit" />
<input type="hidden" name="id" value="9" />
<input type="submit" />
<input type="hidden" name="id" value="11" />
<input type="submit" />
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
You generate only one form. When you submit that form you will send all the ids on the same field name "id". This means only the last id will be stored in $_POST['id']. This explains the "the last row is updated, not the first one".
To fix your problem you have to create individual forms so they will look similar to this:
<form action="post">
<input type="hidden" name="id" value="5" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="8" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="9" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="11" />
<input type="submit" />
</form>
<form action="post">
<input type="hidden" name="id" value="21" />
<input type="submit" />
</form>
So opening and closing the form must be happening inside the while loop.
I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>
i m using isset to get the button value clicked using hidden type...below is my code
<form id="posform" name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="1" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos" value="2" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos" value="3" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos" value="4" /><input type="submit" value="Update" ></div>
</form>
if (isset($_POST['updatepos'])){
$targetbtnvalue=$_POST['updatepos'];
echo $targetbtnvalue;
}
but with below code irrespective of the button clicked it always echos the last button value i.e 4.
i am trying to get it to echo the corresponding value of button clicked...plz guide
That's because it's the same form with the same field.
Try this instead:
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="1" /><input type="submit" value="Update" ></div>
</form>
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="2" /><input type="submit" value="Update" ></div>
</form>
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="3" /><input type="submit" value="Update" ></div>
</form>
<form name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos" value="4" /><input type="submit" value="Update" ></div>
</form>
All of your hidden input names are same i.e updatepos which shouldn't be. So you can try with different names for hidden inputs or make it array like this way updatepos[]. Then you'll get all the hidden input values on your $_POST array not just the last one.
form id="posform" name="posform" action="" method="POST" >
<div><input type="hidden" name="updatepos[]" value="1" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos[]" value="2" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos[]" value="3" /><input type="submit" value="Update" ></div>
<div><input type="hidden" name="updatepos[]" value="4" /><input type="submit" value="Update" ></div>
</form>
and then with PHP try like this,
if (isset($_POST['updatepos'])){
$targetbtnvalue=$_POST['updatepos'];
print '<pre>';
print_r($targetbtnvalue);
print '</pre>';
}
I am working on a php situation that i have. What it should do is like. each product has a unique id value. if the product id is posted throught submit button, A count action should be activited to count the number of time that product id is posted, so increment.
I hope i exposed the situaation clearly. This is the way i thought doing it, but can't get it work:
<?php
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do;
if ($do > 1)
{
$i= $do+1;
echo $i;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>test</title>
</head>
<body>
<div class="holder">
<div class="im">
<img src="session-test/images/orange-juice.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="2" />
<input type="hidden" id="price" name="price" value="25" />
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
<div class="im">
<img src="session-test/images/milkshake.jpg" />
<p>bestorange-juice</p>
<form method="post" action="sessiontest.php">
<input type="hidden" id="id" name="id" value="3" />
<input type="hidden" id="prrice" name="price" value="1" />
<!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>-->
<input type="submit" value="send value" name="submit" id="submit" />
</form>
</div>
</div>
</body>
</html>
If I assume the value in the hidden input #id is the times users vote the product:
if (isset($_POST['submit'])) {
$do = count($_POST['id']);
echo $do++;
}
If I assume it incorrectly I don't understand what you want to do, sincerly.
can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>