Can't get the selected value from a drop down menu - php

I have this piece of code in my PHP file to project the contents of a txt file as a drop down menu.
echo '<p style="text-align:center;"><form action="schedule.php" method="POST" name="theForm2" id="theForm2"></p>
<p style="text-align:center;"><select name="fh[]"></p>';
foreach($lines as $line) {
echo '<option value="'. urlencode($line).'">'.$line.'</option>';
}
echo '</select>
<input type="submit" name="scheduleButton" value="Schedule" />
<input type="submit" name="deleteButton" value="Delete" />
</form>';
And then i use this to get the selected value.
if (isset($_POST['deleteButton'])) {
$v = ($_POST['deleteButton']);
}
But the problem is that every time i get v = "Delete".
Any help would be much appreciated.

That's because the value of the deleteButton control is "Delete". The code is doing exactly what you asked it to.
If you want the value of the select control, you need $_POST["fh"] and to change your select to <select name="fh">.

Related

pass to 2 different php pages selected dropdown value. Not javascript

I am looking for only PHP. NOT javaScript.
I have a drop down which has 1 of the 15 week. If i chooses any value from that drop down it should go to selected page and say..
Welcome you have selected week #
<?php
if (isset($_POST['attendance'])) {
header('Location:attendance_page.php');
} else if (isset($_POST['handout'])) {
header('Location:handout_page.php');
} else if (isset($_POST['assignment'])) {
header('Location:assignment_page.php');
}
?>
<form action="#" method="post" >
<p>Welcome Professor Mr.XYZ !</p><br/><br/>
<select name="sweek">
<?php
for($i=1;$i<=15;$i++)<!--i am populating dropdown with 15 weeks-->
{
echo "<option value='Week$i'> Week $i</option>";
}
?>
</select><br/><br/>
<input type="submit" name="attendance" value="Attendance" />
<input type="submit" name="handout" value="Handout" />
<input type="submit" name="assignment" value="Assignment" />
</form>
Can anyone help please
To post to two differenet pages you need to post to one and then redirect to the other. But you can't redirect with post data in PHP.
You'd need to use get instead of post. To transform post to get you can do:
$get = http_build_query($_POST) . "\n";
header('Location: otherpage?' . $get);

get selected value of a drop down which is populated with results from an SQL query

So I have a drop down populated with the names based on an SQL query. I want to be able to see which option the user selected before they pressed submit and use this as a variable on a separate php file. I assume I will need to use session variables? I'm a bit lost so any help would be appreciated. I have the following code so far:
<form name="ClientNameForm" id="ClientNameForm" action="ClientDetails.php">
<input type="text" name="ClientName" id="ClientName" placeholder="Type Service User's name here:" style="width: 200px"/><br/><br/>
<select name="Name_dropdown" id="name_dropdown" style="width: 200px" >
<?php
$ClientName_Query= "SELECT CONCAT(FName, ' ', SName) AS FullName FROM ClientDetails";
$ClientName_Result= mysql_query($ClientName_Query) or die (mysql_error());
while ($row= mysql_fetch_array($ClientName_Result)){
echo "<option> $row[FullName] </option>";
}
?>
</select><br/><br/>
<input type="submit" name="submit_btn" id="submit_btn" value="Submit"/>
</form>
In your ClientDetails.php file the value will be available using,
$name = $_POST['Name_dropdown'];
If you need to change a setting in the form document before submitting you can use jQuery. Something like
$('#name_dropdown').change(function(){
var option = $(this.options[this.selectedIndex]).val();
});

GET method with another variable already attached

I need to pass a few GET variables through. The link I need is
index.php?type=door&qty=4
I can write this easily enough like
But I need to select the $qty amount fron a drop down list. So I have
<form name="qty" method="GET" action="../../../index.php?door-type="<?php echo $door_model; ?>">';
<select>
<?php
for ($front_int=1; $front_int<=99; $front_int++)
{
echo'<option value="'.$front_int.'">'. $front_int . '</option>';
}
?>
</select>
<input type="submit" value="front-qty">
</form>
So how can I append these two values into one so that I get then $_GET them on the next page like I would with that first link I created?
If qty needs to be a select, then give that name to the select. Also, pass the other variable with a hidden field, which is pretty standard. Good luck :)
<form name="NOT_QTY" method="GET" action="../../../index.php">
<input type="hidden" name="door-type" value="<?php echo $door_model;?">
<select name="qty">
<?php
for ($front_int=1; $front_int<=99; $front_int++)
{
echo'<option value="'.$front_int.'">'. $front_int . '</option>';
}
?>
</select>
<input type="submit" value="front-qty">
</form>

PHP List of records from filemaker select button

First, thank you for reading this.
I am just starting php and I am tying to make a site using FileMaker to display and enter information.
I have the php connecting to my database, then a search page using a form, then it displays a list of records. I would like to make a "button" that will select one record then display related records.
This is where my trouble is. I do not know how to make a form that will save either the record_Id or key field to then display the next page.
I am using a foreach loop to display the list in a table:
$records = $result->getRecords();
echo '<table border="1">';
echo '<tr>';
echo '<th>Company</th>';
echo '<th>Id Num</th>';
echo '<th>Choose</th>';
echo '</tr>';
foreach ($records as $record) {
echo '<tr>';
echo '<td>'.$record->getField('Company').'</td>';
echo '<td>'.$record->getField('K_Medical').'</td>';
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
As you can see I have tried to use a hidden form field to get the key field of the record, but the page dose not work. I get an error 500 when I try to view it in a browser.
Any help would be greatly appreciated! If I have not provided enough information please let me know.
Replace :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='$record->getField('K_Medical')/>';
<input type="submit" />
</form>';
By :
echo '<td>
<form action="welcome.php" method="post">
#This is where I think I need the button, but instead it just breaks :(
<input type="hidden" name="med_id[]" value='.$record->getField('K_Medical').'/>
<input type="submit" />
</form>';
You have a quotes and concatenation errors.

Multiple Submits on a page

I have this PHP code:
$result = mysql_query("SELECT distinct om_quote_no from `porders` order by om_quote_no desc") or die(mysql_error());
echo '<select name="project_no">';
while ($row = mysql_fetch_array($result)) {
echo "<option value=".$row['om_quote_no'].">".$row['om_quote_no']."</option>";
}
echo '</select>';
?>
<input type="submit" value="Submit" />
</form>
<div id="table">
<?php
if($_GET){
So as you can see the PHP is forming a dropdown input and once submitted its going to execute some code, but what i need to do is have two dropdown inputs and therefore two submit buttons, however i'm not sure how to form the PHP if statement to distinguish which submit was pressed, so i'll have(pseudo):
if (submit1){
}
if (submit2){
}
Is that possible?
If you give your <input type="submit"> elements names, the one that is clicked will have its name and value sent to the server.
<input type="submit" value="Submit" name="submit1">
if clicked will send submit1=Submit to the server. You could therefore check with if ($_GET['submit1']) to see if it was pressed.
This isn't the best way but can do something like this too:
<select name="test" onchange="document.location ='test.php?submit=dropdown1'">
<option>test</option>
<option>test1</option>
</select>
<br><br>
<select name="test1" onchange="document.location ='test.php?submit=dropdown2'">
<option>test2</option>
<option>test3</option>
</select>
within test.php file:
if($_GET['submit'] == 'dropdown1')
{
print "One";
//statements to execute
}elseif($_GET['submit'] == 'dropdown2'){
//statements here to execute
print "Two";
}
<input type="submit" value="Submit 1" name="submit1"/>
<input type="submit" value="Submit 2" name="submit2"/>
<?php
if(isset($_POST['submit1'])){
//do stuff
//grab select option
$select_option=$_POST['project_no'];
}else if(isset($_POST['submit2'])){
//do stuff
}else{
//do stuff
}
?>
Or if your method is GET than change $_POST to $_GET :)

Categories