multiple onchange submit from a while in php not working - php

I have a table in HTML that gets built from a WHILE () { }
inside this table, I have a drop down menu and on select, I have the onchange= command to execute a part of PHP code above on same file. Everything works well, except when I have multiple result in my while because I can't distinguish each form ... how can I do this.
I have a hidden value. but how do I manage this from the if (isset($_POST['status'])) stand point ?
SAMPLE CODE
<?php
if (isset($_POST['status']))
{ }
?>
HTML HERE
<?php
while ($bookingrequest2 = mysql_fetch_array( $bookingrequest ))
{
echo "<tr><td>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" name=\"reply\">
<select name=\"status\" class=\"form-field\" onchange=\"reply.submit();\" >
<option selected value=\"0\">Make a Selection</option>
<option value=\"Approve\">Approve</option>
<option value=\"1\">Modify</option>
<option value=\"Decline\">Decline</option>
</select>
<input type=\"hidden\" value=\"".$bookingrequest2['booking_id']."\" name=\"booking_id\"/>
</form></td></tr>";
}
?>

Add a hidden input field that identifies the database row. And change reply.submit() to this.form.submit(), because you can't refer to reply as a single element when there are multiple forms with the same name.
while ($bookingrequest2 = mysql_fetch_array( $bookingrequest ))
{
echo "<tr><td>
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" name=\"reply\">
<select name=\"status\" class=\"form-field\" onchange=\"this.form.submit();\" >
<option selected value=\"0\">Make a Selection</option>
<option value=\"Approve\">Approve</option>
<option value=\"1\">Modify</option>
<option value=\"Decline\">Decline</option>
</select>
<input type='hidden' name='id' value='{$bookingrequest2['id']}'>
</form></td></tr>";
}
Then the form processing code can do:
$booking_id = $_POST['id'];

Related

PHP Code first saves the input, but when clicked on another button it doesn't save it anymore

I wrote a Rock Paper Scissors code that first gets the input of player 1, it supposed to save it and then asks for the input of player 2, saves it and outputs the inputs of both the players.
But right now it only outputs the input of player 2, but if you enter the first input it still saves it, but after entering the second one is just doesn't save it anymore. Could someone please help me fix this?
<?php
if (!isset($keuze1)) {
echo "
<form method='GET'>
<input type='submit' name='knop' value='Begin met spelen'></button>
</form>
";
}
$knop = '';
if (isset($_GET['knop'])) {
echo "
<h2>Speler 1</h2>
<form method='GET'>
<select name='speler1'>
<option value='steen'>Steen</option>
<option value='papier'>Papier</option>
<option value='schaar'>Schaar</option>
</select>
<input type='submit' name='keuze1' value='kiezen'>
</form>;
";
}
$keuze1 = '';
$keuze2 = '';
if (isset($_GET['keuze1'])) {
$keuze1 = $_GET['speler1'];
echo "
<h2>Speler 2</h2>
<form method='GET'>
<select name='speler2'>
<option value='steen'>Steen</option>
<option value='papier'>Papier</option>
<option value='schaar'>Schaar</option>
</select>
<input type='submit' name='keuze2' value='kiezen'>
</form>
";
}
if (isset($_GET['keuze2'])) {
$keuze2 = $_GET['speler2'];
echo "
Speler 1 koos $keuze1 en Speler 2 koos $keuze2
";
}
echo $keuze1;
?>
Every time you submit a form, the script starts over, and nothing is "saved" from one iteration to the next. So when you click the <input type='submit' name='keuze2' value='kiezen'> button, everything is reset and the only variables you have are the ones that are submitted in the form.
So if you want to bring $keuze1 along, the easy way to do that is to pass it into a hidden form field, like this:
if (isset($_GET['keuze1'])) {
echo "
<h2>Speler 2</h2>
<form method='GET'>
<input type='hidden' name='speler1' value=" . htmlspecialchars($_GET['speler1']) . " />
<select name='speler2'>
<option value='steen'>Steen</option>
<option value='papier'>Papier</option>
<option value='schaar'>Schaar</option>
</select>
<input type='submit' name='keuze2' value='kiezen'>
</form>
";
}
... and then, get $keuze1 from $_GET['speler1'] in your final if block, before outputting it.

get value from combo box to php

this is my combo box page coding:
<?php
echo "<form method=post align=center>
<div id=myDiv>
<select name=userselect>
<option value=empty></option>
<option value=Confirm> Confirm </option>
<option value=Processing> Processing </option>
<option value=Pending> Pending </option>
<option value=Cancelled> Cancelled </option>
</select>
<button type=button name=combobox value=combobox onclick=loadXMLDoc()>Update</button>
</div>
</form>";
?>
this is my php code (another page):
<?php
if (isset($_POST['combobox']))
{
$userselect = $_POST['userselect'];
echo $userselect;
}
?>
if user select one value in combo box that value should store in db and then display what they are selected. for example if user select CONFIRM option that value stored in db after that it display CONFIRM instead of that combo box form and button. Here i can stored the combo box values in db successfully. thats not a problem and then here i used ajax method to show the msg instead of button place. Now, i want what user select it should display the same value. check these above coding and reply me ur suggestions...
Replace Your code with this:
<?php
//Connect To your Database File..
//After Connecting To Databse
$combo = mysqli_real_escape_string('yourconnectionlink', $_POST['userselect'])
echo "<form method='post' action='youractionfile.php' align='center'>
<div id='myDiv'>
<select name='userselect'>
<option value='empty'></option>
<option value='Confirm'> Confirm </option>
<option value='Processing'> Processing </option>
<option value='Pending'> Pending </option>
<option value='Cancelled'> Cancelled </option>
</select>
<input type='button' name='combobox' value='combobox'>Update</button>
</div>
</form>";
//Insert Data into Database File
if($_POST['userselect'])) {
if(isset($_POST['userselect'])) {
$query = "INSERT INTO tablename (your field names) VALUES ('".$combo."')";
$res = mysqli_query('yourconnectionlink', $query);
echo '1 row inserted';
}
}
?>
finally i got it what i want. without using ajax method i can store the data to db and then combobox, button hide permanently if once we updated the status. this is my final coding...
if ( $a_row['status'] != 'empty' ) {
echo "\t<td>" . $a_row[$status] . "</td>\n";
}
else {
echo "\t<td><form action=statusdb.php method=post>
<select name=update>
<option value=empty></option>
<option value=Confirm>Confirm</option>
<option value=Processing>Processing</option>
<option value=Pending>Pending</option>
<option value=Cancelled>Cancelled</option></select>
<input name=id type=hidden value='".$a_row['slno']."';>
<input type=submit value=Update>
</form>
</td>\n";
}
statusdb coding:
if (isset($_POST['id']))
{
$id = mysql_real_escape_string($_POST['id']);
$update= mysql_real_escape_string($_POST['update']);
$sql = mysql_query("UPDATE guest_details SET status = '$update' WHERE slno = '$id'");
if(!$sql)
{
die("Error" .mysql_error());
}
}
i posted another question (php combobox & button should hide once updated into mysql db and show success message instead of combobox & button place.). i got help from that answer... so, only i posted the correct answer here..

Get the Value First Before the Form Shows up PHP

I'd like to show up to 10 email fields to submit on the form but only by selecting options from 1 - 10.
<form action="" method="post" name="select">
<select name="row">
<option value=1">1</option>
...
<option value="10">10</option
<input type="submit" name="select">
</form>
The next form shows up depending on the selected form
<form name="secondform">
<?php ... ?> <!--for loop depending on the value above-->
<input name="email>
</form>
So you have your dropdown, submit that value to either self or another php script that contains this:
$selected = $_POST['select'];
for($i = 0; $i<$selected; $i++){
echo "<input type='text' name='email$i'>";
}
$selected is the value of the dropdown menu.
Now we have a for loop. It set's $i to zero then says while $i is less than the value of your drop down value, add 1 more to $i and run echo "<input type='text' name='email$i'>";
As you can see, I appended $i to to email so that each field has it's own unique name.
What you can do as well is do just as CP510 says and add onchange='this.form.submit()' to the select tag so it is submitted automatically and I would submit the form to the same page. This meaning set the action of the form of which the select menu is in to action="<?php echo $_SERVER['PHP_SELF']; ?>".
I would STRONGLY recommend learning jQuery for something like this.
Here's the jQuery (mostly just plain javascript) version of this... it's very similar:
Please make sure you put this in the head of your html file:
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
HTML:
<select id='selected' onchange='addFields();'>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
</select>
Javascript:
function addFields(){
var selected = $('#selected').val();
for(var i = 0; i<selected; i++){
document.write('<input type="text" name="email' + i + '">');
}
}

<select> dropdown default value

I have this code:
if(isset($_POST['search']))
{
$res1=mysql_query("SELECT * FROM aircraft where acode = '$_POST[ac]'") or die(mysql_error());
while($row=mysql_fetch_array($res1))
{
$airc=$row['acode'];
$amode=$row['amodel'];
$stat=$row['status'];
$rem=$row['remarks'];
echo "<center><table><form name=\"frmMain\" method=\"post\">
<tr><td><font face=consolas><b>Aircraft Code:</b></font></td><td><input type=text name=arc value='$airc' readonly=readonly></td></tr>
<tr><td><font face=consolas><b>Aircraft Model:*</b></font></td><td><input type=text name=am value='$amode'></td></tr>
<tr><td><font face=consolas><b>Status:*</b></font></td><td><input type=text name=st value='$stat'></td></tr>
<tr><td><font face=consolas><b>Remarks:*</b></font></td><td><input type=text name=rm value='$rem'></td></tr></table>";
}
}
On submit 'search' button, this code displays the data from aircraft table. The user is allowed to update the data with the (*) sign.
Since the Status are the following by default (Available, Not Available), I changed this
<tr><td><font face=consolas><b>Status:*</b></font></td><td><input type=text name=st value='$stat'></td></tr>
to this,
<tr><td><font face=consolas><b>Status:*</b></font></td><td><select name=st>
<option value=Available>Available</option>
<option value='Not Available'>Not Available</option>
</select></td></tr>
But I want the dropdown to have it's default value depending on
$stat=$row['status']; since this is an update form.
If the data being retrieved has it's status 'Available', then the dropdown should have it's default value as 'Available'.
How can I achieve that? I tried <select name=status value='$stat'> but it doesn't work. Any help will be appreciated. Thanks!
Just put selected="selected" on the option depending on your $row['status'],
<option selected="selected" value="available">Available</option>
write Available and Unavailable into an array
$theArray = array("Available","Not Available");
loop the array:
<tr><td><font face=consolas><b>Status:*</b></font></td><td><select name=st>
<?php
foreach ($theArray as $key => $value) {
if ($value == $stat) {
echo('<option selected="selected" value='.$value.'>'.$value.'</option>');
} else {
echo('<option value='.$value.'>'.$value.'</option>');
}
}
?>
</select></td></tr>
and in each loop we check if the value in the array, is the same as it is in the variable, if so, we put the selected there
understand the logic?
<select name=status>
<option value="available" <?php if($row['status']=="available") echo "selected=\"selected\""; ?>>Available</option>
<option value="unavailable" <?php if($row['status']=="unavailable") echo "selected=\"selected\""; ?>>Unvailable</option>
</select>
Basically echo selected="selected" for the option depending on value of the concerned field.
<?php
$status = "navail";
?>
<select name="sel">
<option value="avail" <?php if($status == "avail") echo "SELECTED";?> > Avail </option>
<option value="navail" <?php if($status == "navail") echo "SELECTED";?> > Navail </option>
</select>
You can define your variable value with additional option tag and mark that as selected like:
<select name="role" id="role">
<!-- This is default define value using php variable $r -->
<option selected="selected" value="<?php echo $r; ?>" disabled="disabled"><?php echo $r; ?></option>
<!-- Other options values -->
<option value="2">Option-2</option>
<option value="2">Option-2</option>
</select>
You can set the selected dropdown option from database as below:
<select name="status">
<option <?php echo ($row['status'] == 'Available') ? 'selected' : '' ?> value='Available'>Available</option>
<option <?php echo ($row['status'] == 'Not Available') ? 'selected' : '' ?> value='Not Available'>Not Available</option>
</select>
Declare the options in an array like
$arr = array("available" => "available","unavailable" => "unavailable");
and input the drop down like this
echo form_dropdown("st", $arr, set_value("st", (isset($row['status'];) ? $row['status']; : ""))
This is the method commonly used in frameworks like codeigniter.. i think it works in core php too..
its my first time here though and i tried using basic principles in php, dont know if this helps you but if you're trying to grab the defaulted value which you inputted on your database then edit it again i suggest you try this one
<select name="st">
<?php if($row['status']=="Available"){?><option value="Available">Available</option><?php }?>
<?php if($row['status']=="Unavailable"){?><option value="Unavailable">Unavailable</option><?php }?>
</select>
Assuming that the column name in your database is 'status', give it a try works for me

How to Keep the selected value of the select box after Form POST or GET

Im trying to implement the search feature in my website.
when the search keyword is entered in the textbox, and the category combo is selected, the form will be Posted and the result will be shown on the same page.
what i want is to keep the selected category of the combo by default in the form after posted
For eg., If i select the category 'Automobiles' in the combo and click search, after form submit, the combo should show the automobiles as default selected option. Please help me. Any help will be appreciated
I assume you get categories from database.
you should try:
<?php
$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>
Assuming that by "combo" you mean "A regular select element rendering as a drop down menu or list box" and not "A combobox that is a combination of a drop down menu and free text input":
When outputting the <option> elements, check the value against the submitted data in $_POST / $_GET and output selected (in HTML) or selected="selected" (in XHTML) as an attribute of the option element.
Here is the JQuery way I am using.
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
$("#name").val("<?php echo $_POST['name'];?>");
</script>
But this is only if you have jquery included in your webpage.
Regards
<?php
$example = $_POST["friend"];
?>
<form method="POST">
<select name="friend">
<option value="tom" <?php if (isset($example) && $example=="tom") echo ' selected';?>>Thomas Finnegan</option>
<option value="anna" <?php if (isset($example) && $example=="anna") echo ' selected';?>>Anna Karenina</option>
</select>
<br><br>
<input type="submit">
</form>
This solved my problem.
This Solved my Problem. Thanks for all those answered
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
$countries_uid = $_POST['countries_uid'];
while($row = mysql_fetch_array($result)){
$uid = $row['uid'];
$country = $row['country_name'];
$isSelected = null;
if(!empty($countries_uid)){
foreach($countries_uid as $country_uid){//cycle through country_uid
if($row['uid'] == $country_uid){
$isSelected = 'selected="selected"'; // if the option submited in form is as same as this row we add the selected
}
}
}else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$uid."'".$isSelected.">".$country."</option>";
}
this is my solutions of multiple select dropdown box after modifying Mihai Iorga codes
After trying al this "solves" nothing work. Did some research on w3school before and remember there was explanation of keeping values about radio. But it also works for Select option. See here an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
Easy solution:
If select box values fetched from DB then to keep selected value after form submit OR form POST
<select name="country" id="country">
<?php $countries = $wpdb->get_results( 'SELECT * FROM countries' ); ?>
<option value="">
<?php if(isset($_POST['country'])){echo htmlentities($_POST['country']); } else { echo "Select Country *"; }?>
</option>
<?php foreach($countries as $country){ ?>
<option <?php echo ($_POST['country'] == $country->country_name ? 'selected="selected"':''); ?> value="<?php echo $country->country_name; ?>"><?php echo $country->country_name; ?>
</option>
<?php } ?>
</select>

Categories