I need to get the value of the selected option if the checkbox is checked. For example, I checked the checkbox in the third row. In another page I want to get the value of the third row selected option. Please don't use the $a to get the data.
<form method = "post" action = "">
<table>
<?php
for($a = 0;$a <=3; $a++){
echo '<tr><td>';
echo '<input type = "checkbox" name = "chckbx">';
echo '</td><td>';
echo '<select name = "slct1">';
echo '<option>';
echo $a;
echo '</option>';
echo '</select>';
echo '</td></tr>';
}
?>
</table>
Related
I am making a student sign-up form using html and php.
First you are asked to insert your name, password and email and when you click submit, it takes you to another page (ChooseDepartment.php) in which you get a list of all departments from my database to choose your own.
Now, I am a total newbie, so here is the part of my php code that I am stuck with in ChooseDepartment.php:
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo </form>;
}
I am trying to make the value of the radio button carry the value of the
the department id so I can then update my database with the student's department which is currently NULL, but I can't figure out how to use both html and php at the very same line correctly! This gives me syntax error!
as you're in PHP, so you don't need to open and close PHP tag.
The reason you're getting Syntax error is just because you're not manipulating string properly.
error is with this line
echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';
^ here ^ here
So you need to remove the PHP tags and need to concatenate string properly like:
echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';
and with this one
echo </form>;
you're missing quotes around form tag. So it should be,
echo '</form>';
There are some other typos are as well, so your final code will be look like this.
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
//echo "<br />"; add this <br /> tag to next echo
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
//or you can do this way
//echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
//echo "<br />"; appended in upper statement.
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
//echo </form>; closed already(above statement).
}
and without comments, more cleaner :)
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}
Take a look at string operators
http://php.net/manual/en/language.operators.string.php
You can combine two strings in php with a dot, so that part of your code would become this:
{
echo $row['NAME'];
echo '<input type="radio" name="department" value="'.$row['DEPT_ID'].'">';
echo "<br />";
}
No need to open php tag again
$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0) {
echo "<br />"."Available departments: "." ".mysql_num_rows($ShowPossibleDep)."<br />";
echo "<br />";
echo '<form id = "dept" action = "Courses.php" method = "post">';
while ($row = mysql_fetch_array($ShowPossibleDep)) {
echo $row['NAME'];
echo '<input type="radio" name="department" value="' . $row['DEPT_ID'] .'">';
echo "<br />";
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment">';
echo "</form>";
}
I have a loop that creates options in a drop down form.
How can I pass the variable $objectID[$i] from the loop where $i consistent with the selected value $i
echo '<form action="#" method="post"><select name="Restaurant">';
for ($i = 0; $i < count($restaurants); $i++){
$name[$i] = $restaurants[$i]->get("Name");
$city[$i] = $restaurants[$i]->get("City");
$objectID[$i] = $restaurants[$i]->getObjectID();
//echo '<input type="hidden" name="passRestaurant" value="' . $name[$i]. '" />'; // tried this, but it just messes up the format of the drop down
echo "<option>{$name[$i]} -" . " {$city[$i]}</option>";
}
echo '</select><br><br><input type="submit" name="submit" value="Next" />
</form>';
}
This prints the value that was selected, but I also want to print the $objectID[$i] at the same $i value:
if(isset($_POST['submit'])){
$selected_val = $_POST['Restaurant']; // Storing Selected Value In Variable
echo "You have selected :" .$selected_val; // Displaying Selected Value
}
why you just put $objectID[$i] in
echo "<option value=\"{$objectID[$i]}\">{$name[$i]} - {$city[$i]}</option>";
so you can get all the properties of Restaurant with $objectID
I'm recalling query values which are settings alongside their parameters. this includes the ID, value and description. As the fetch is in a form of a array i used a loop to display the different variables (this way if there are more settings to put in it is easier to expand). The loop consists of a different form allowing you to edit the value of the parameter of that setting. When you submit, it does not post the value, but it posts the ID attached to the button. What can I do?
$query = "SELECT * FROM metadata";
$db->DoQuery($query);
$num = $db->fetchAll(PDO::FETCH_NUM);
if (isset($_POST)){
echo "<pre>";
print_r($_POST);
echo $_POST['service_name'];
echo "</pre>";
}
echo "<table id='mytable' style='width:100%'>";
echo "<tr>
<th>Option</th>
<th>Value</th>
<th>Description</th>
</tr>";
foreach ($num as $row) {
echo '<form action="" method="post">';
echo '<tr>';
echo '<td>'.$row['rule'].'</td>';
echo '<td><input name="service_value" placeholder="value" value='.$row['value'].'></td>';
echo '<td>'.$row['description'].'</td>';
echo '<td><button value="'.$row[0].'" name="service_id"></button></td>';
echo '</tr>';
}
?>
</form>
</table>
I have several drop down menus which are static.
Upon selecting an option in either drop down a respective php variable is updated with a value.
Those php variables will ultimately be used later on in a sql query to display records based on the multiple selections.
Each dropdown is an html select contained inside its own form tags. Forms are submitted as such
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo '<select onchange="this.form.submit()" name="numberSelect" >';
This method is simple but does not retain the values posted from one form as soon as you submit another.
Is there a way to retain the values of multiple selects in php variables or some other method so that values from multiple forms can be used elsewhere in php?
this working snippet of php illustrates the issue,
<?php
//read in selected value
$SelectedColor = $_POST['colorSelect'] ? $_POST['colorSelect'] : 'none';
$SelectedNumber = $_POST['numberSelect'] ? $_POST['numberSelect'] : 'none';
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo '<label>Selected Color: </label>';
echo $SelectedColor ;
echo '<br/>';
echo '<select onchange="this.form.submit()" name="colorSelect">';
echo '<option value="none">Choose</option>';
$color_options = array('red','blue','yellow');
foreach($color_options as $option) {
$selected = ($option == $SelectedColor) ? 'SELECTED' : '';
echo "<option $selected value='$option'>$option</option>";
}
echo '</select>';
echo '</form>';
?>
<br/>
<?php
$number_options = array(1 => 'one', 2 => 'two', 3 => 'three');
echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">';
echo '<label>Selected Number: </label>';
//if a number was posted get the corresponding string
echo is_numeric($SelectedNumber) ? $number_options[$SelectedNumber] : $SelectedNumber;
echo '<br/>';
echo '<select onchange="this.form.submit()" name="numberSelect" >';
echo '<option value="none">Choose</option>';
foreach($number_options as $option => $value) {
$selected = ($value == $SelectedNumber) ? 'SELECTED' : '';
echo "<option $selected value='$value'>$option</option>";
}
echo '</select>';
echo '</form>';
?>
Remove first
</form>
And the 2nd
<form>
Both selects need to be part of the same form.
I don't know if this can be done in php. I have an equipment list that is call from database. User can select any equipment that they want to borrow by selecting the checkbox. I have checkbox named 'list'. The question is how to put checkbox value and post the checkbox value?? because I want to save the selected item(checked) into database.
<?php
//include("connect.php");
$sql = mysql_query("SELECT * FROM equipment WHERE equip_name_desc='$search'");
$row = mysql_num_rows($sql);
if ($row >= 1){
echo '<table border="1"><tr>';
echo '<td align="center" width="40">NO</td>';
echo '<td width="200">DESCRIPTION</td>';
echo '<td width="120">SERIAL NUM.</td>';
echo '<td width="120">REF. NUM.</td>';
echo '<td width="120">PRICE</td>';
echo '<td width="120">STATUS</td>';
echo '<td width="80">BORROW</td>';
echo '</tr>';
$index=1;
while($a = mysql_fetch_array($sql))
{
echo '<tr>';
echo '<td align="center">'.$index.'</td>';
echo '<td>'.$a['equip_desc'].'</td>';
echo '<td>'.$a['equip_sn'].'</td>';
echo '<td>'.$a['equip_ref'].'</td>';
echo '<td>'.$a['equip_unit_price'].'</td>';
echo '<td>'.$a['equip_status'].'</td>';
echo '<td>'?><input type="checkbox" name="list" /><?php
echo '</tr>';
$index++;
}
echo '</table>';
}
?>
Well, pretty weird thing, but ill try to understand. I suppose the serialnumber is the unique primary. So you could use that as a reference for all the items in the array:
echo '<td><input type="checkbox" name="list" value="$a[\'equip_sn\']></td>';
this is how you put the checkbox value
echo '<td>'?><input type="checkbox" name="list" value="'.$a['your field'].'" />
to post the check box value
assign it to a variable
$variablename=$_POST['list']=="$a[0]" //take the values stored in the array