So I have a form which has a drop down list, when the submit button is pressed the script task4.php is called. My problem is that I can use the whole option that is selected, however I only need the tid. How do I get just the tid and use it task4.php?
<form action="task4.php" method="get">
<select>
<?php
foreach($results as $row) {
echo "<option>".$row[tid].", ".$row[category].", ".$row[division].", ".$row[clubID].", ".$row[name]."</option>";
}
?>
</select>
<input type ="submit" value="Submit">
</form>
This is what I've got in task 4 and it isn't working:
if (isset($_GET['tid'])) {
$tid = $_GET['tid'];
}
It's possible (perhaps even necessary) that in the absence of a value the browser is sending the text contents of the selected option with the form data.
Just give the option a value:
echo "<option value=\"".$row[tid]."\">" ...
Additionally, that select should really have a name (I don't even see how it could have worked without one):
<select name="tid">
Related
I am trying to create an html form where i have a dropdown menu populated with data from stations table. I am able to select a station and then i want to use this station name when a button is clicked to display a table with related data. But i don't understand how i can use selected station to create another query when submit button is clicked. Below is my code:
<form method="post">
<label>Choose a station:</label>
<select name="owner">
<?php
$sql = mysqli_query($conn, "SELECT name FROM stations");
while ($row = $sql->fetch_assoc()){
?>
<option value="owner1"><?php echo $row['name']; ?></option>
<?php
}
?>
</select>
<br><br>
<input type="submit" value="Submit">
</form>
i think onclick must be used but i don't understand what would be posted that i can use for new sql query.
When you click submit button browser send form via POST method to the same page and reload it. So you can capture the value selected in the form with this PHP code:
If (isset($_POST['owner'])) {
// mysqli query where the selected value is $_POST['owner']
}
Once I add session in edit form, I could not get selected values in dropdown menu. I am using bootstrap framework. When I remove session from the same beginning it works.
edit.php file
<?php
session_start();
$car=$_POST['car']
?>
<form method="post" action="">
<div class=from-group>
<label for="exampleInputEmail1>Car</label>
<select class="form-group" name="car" id="car" value="<?php echo $car; selected?>">
<option value=<?php echo $car?> selected>
<?php cars()?>
</option>
</div>
</form>
I have created "cars" function in which I get values of all cars from database.
function cars(){
$link = new mysqli("", "", "", "");
$link = set_charset("utf8);
$sql = mysqli_query($link, "SELECT * FROM db_cars ORDER BY CarId")
echo '<option value=""> Choose car </option>'
while ($record = mysqli_fetch_array($sql)) {
echo '<option value = "'.$record['CarId']'"> . $record['CarName'].' </option>
}
}
Any help or advice is appreciated.
The select element has no name attribute, so it cannot be a successful control (i.e. submit any data).
The form has no submit button, so there is no obvious way for you to trigger the form submission. (You have to submit the form in a new HTTP request to the server to run the PHP and get the data from the user).
First, add the semicolons after every line when you use PHP. That can make a lot of troubles.
Second, some attributes have just one ("), like the label in the HTML.
Third, you are adding "something" called selected after echoing $car.
In the function cars() you are echoing without a semicolon (;) and just after doing a while() loop.
Check out all, you are missing very important and essential things
Also, I gonna guess this is not a "copy-paste" from your code, but if it's, you also are missing </select>
I' new to HTML and PHP and was wondering if anyone out there could help me with a question... I'm trying to code a form that submits a selection from one menu OR another and, depending on which is chosen sends the data to one of two different PHP pages.
<form id="form1" method="post" action="">
<label for="SubjectID">Text Books by Subject</label>
</p>
<p>
<select "name="SubjectID" id="SubjectID">
<?php do { ?>
<option value="<?php echo $row_rsSubject['SubjectID']?>"><?php echo $row_rsSubject['SubjectName']?></option>
<?php } while ($row_rsSubject = mysql_fetch_assoc($rsSubject));
$rows = mysql_num_rows($rsSubject);
if($rows > 0) {
mysql_data_seek($rsSubject, 0);
$row_rsSubject = mysql_fetch_assoc($rsSubject);
} ?>
</select>
</p>
<p>
——— OR ———
</p>
<p>
<select name="CourseID" id="CourseID">
<?php do { ?>
<option value="<?php echo $row_rsCourse['CourseID']?>"><?php echo $row_rsCourse['CourseID']?></option>
<?php } while ($row_rsCourse = mysql_fetch_assoc($rsCourse));
$rows = mysql_num_rows($rsCourse);
if($rows > 0) {
mysql_data_seek($rsCourse, 0);
$row_rsCourse = mysql_fetch_assoc($rsCourse);
} ?>
</select>
</p>
<p>
<label for="CourseID">Text Books by Course</label>
</form>
So, the action should be (depending on which menu the user selects from) that the form submits to either subject.php or course.php, and I can't figure out how to do that with a single submit button. Can anyone help me?
first set onchange event to select:
<select name="CourseID" id="CourseID" onchange='myFunc(this)'>
Then:
<script>
function myFunc(element){
// set the form action depends on option chosen
// element.setAttribute('action', 'yourPageLink' );
}
</script>
You could update the action attribute with javascript when the user changes his selection, directing him to the relevant page.
Otherwise, why not submit to a single function and depending on the users selection call the relevant function?
Honestly I would use jquery's $.post to do this. To the best of my knowledge you can't really do that with straight html, and php is executed before the page loads so that's no help. With JQuery you could set an action to the button to call a function. Then your function could check the value of the select and pass the proper url to the $.post function.
You can read about that particular jquery function here: http://api.jquery.com/jQuery.post/
If you're not familiar with JQuery it's really easy to use with just a bit of reading if you're good with JavaScript.
Why not have a radio button in the form whose posted form value is processed by a central PHP form handler? That way, depending on what is entered on the radio button, you can process the request as necessary.
This might not be the answer you are looking for, but I am not clear if you want the user to be able to alter the destination or what.
EDIT: My main code no longer works, should this function work?
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script>
var second_choice = $('#second-choice').val();
$("#first-choice").change(function() {
$("$second-choice").load("findModel.php?choice=" + $("#first-choice").val());
});
</script>
Here is the associated PHP File:
<?php
include 'dbc.php';
$choice = mysql_real_escape_string($_GET['choice']);
$query="SELECT * FROM `cars` WHERE `DVLAMake`='$choice'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<option>" . $row{'DVLAModel'} . "</option>";
}
?>
The database connection works.
#
On load a PHP file populates my first dropdown:
<form name="indexSearch" action="searchResults.php" method="POST">
<select id="first-choice">
<option selected value="base">Please Select</option>
<option value="VAUXHALL">VAUXHALL</option>
<?php
$sql="SELECT DISTINCT `DVLAMake` FROM `cars`";
$result = mysql_query($sql);
while ($data=mysql_fetch_assoc($result))
{
echo "<option value =\"{$data[DVLAMake]}\" >{$data[DVLAMake]}</option>\n";
}
?>
</select>
<select id="second-choice">
<option>Please choose from above</option>
</select>
<br />
<input type="submit" style="font-size:14px; padding:3;"value="Submit" size="20" />
</form>
That works, and then on choosing the value it calls a function which then fills the second with options which I can choose from. However when I post the form, it doesn't take the second dropdown selected value through it's just empty but it does take the first dropdown value selected value.
Any reason why?
The problem is that the second select tag does not have name attribute. if it lies in the form you will get the request via post only if the attribute has name. if you are using jquery you can simply fetch value by id then post it via ajax. select like this in jquery.
var second_choice = $('#second-choice').val();
How are you changing the selected value? Using e.g. ajax? And how are you sending your data? Using ajax or simple POST with page reload?
If you're changing something using ajax your data need to be send by ajax. Because without it you will don't send your data generated by JS but this generated by your PHP script.
You wrote
$("$second-choice").load("findModel.php?choice=" + $("#first-choice").val());
instead of:
$("#second-choice").load("findModel.php?choice=" + $("#first-choice").val());
$ -> #
Let's say I have a HTML form:
USA
CDN
And I select option 1 (USA)
Then a php page
Ok, duh, works fine and echo's "1"
How can I display "USA" as well? So in essence, I want to pass along the option's TEXT also. How could I do this?
You would either need to change the option value on the HTML page to be the text you want displayed (poses a XSS security hazard) or on the page that's displaying the text, you'd need an array where all the values match with the forum input.
Ex:
$names[1] = "USA"; $names[2] = "CDN";
Then when you want to display it, you'd call $names[$selection] to output the text.
Another option you can consider is using javascript to update a hidden HTML element on the submitting page when the user makes their selection (using onUpdate). This information would be passed along with the submitted form data. Not the most secure or reliable of options, but an option nonetheless.
The client's browser will not post back anything beyond the form element's name and value - you would have to incorporate additional form fields and Javascript (or change the element's value) to post "USA".
I suggest having a copy of the same data structure you used to print the form.
For example, if you were to do it all in one PHP page...
<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
<select name='country'><?php
foreach($countries as $key=>$country){?>
<option value='<?php echo $key;?>'><?php echo $country;?></option>
<?php
} ?>
</select>
<input type='submit'>
</form>
<?php
if(isset($_POST['country'])){
$chosen=(int)$_POST['country'];
if(!isset($countries[$chosen])){?>Unknown country selection, wtf<?php exit; }?>
<div style='margin-top:20px;'>You selected <?php echo $countries[$chosen];?></div>
<?php }
Another option is to ditch the numeric value altogether, and just use the country name as the value of the option. However, for verification, you'll still want the list of values. Check the differences in this example...
<?php
$countries=array('USA','CDN');
?>
<form action='?' method='post'>
<select name='country'><?php
foreach($countries as $country){?>
<option value='<?php echo $country;?>'><?php echo $country;?></option>
<?php
} ?>
</select>
<input type='submit'>
</form>
<?php
if(isset($_POST['country'])){
$chosen=preg_replace('/[^\w]/','',$_POST['country']);//this isn't strictly necessary, since the next line checks if the value is in your original array - but filtering input is a good habit!
if(!in_array($_POST['country'],$countries)){?>Unknown country selection, wtf<?php exit; }?>
<div style='margin-top:20px;'>You selected <?php echo $chosen;?></div>
<?php }