I have a form which only has one selector in it whos options are mostly populated by php which currently get submitted by <input type="submit" name="courseSubmit"/>
echo '<form name="CourseForm" action="" method="post">';
echo '<select name="CourseSelect"><option value="0">Select Course</option>';
$result = mysqli_query($con," SELECT * FROM course c ");
while($row = mysqli_fetch_array($result))
{
echo "<option value='".$row['IDNum']."' ".$select.">" . $row['Title'] . "</option>";
}
echo '</select>';
echo '<input name="CourseSubmit" type="submit"/></form>';
Upon clicking the button the page is submitted then by using $_POST['CourseSelect'] i can get the value of the select.
However when I try replacing the functionality of the submit button with onchange="this.form.submit()" inside of my select the form does not appear to be submitted
I know I can call js functions from php for example
echo "<select onchange="myjsFunction()">";
there a way using jquery to submit the form in the exact same manor as using <input type="submit"/> ?
You could do something like:
$('select[name=CourseSelect]').change(function(){
$(this).closest('form').submit();
});
And ignore the onchange attribute entirely for the HTML element.
Here's a recursive example:
http://jsfiddle.net/erXxd/1/
Related
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">
This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";
I'm learning PHP and I need help to "send" informations on an another page.
I read a text file and I put data on 2 arrays :
$nomVille;
$nomFichier;
$index; // index is use for arrays
Now I want to build a drop-down list and a submit button.
The drop-down list : show all items on $nomVille
The button : open "villes.php"
villes.php will have to retrieve $nomFichier and $index but I'm not able to do that.
Here is a part of my code :
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<FORM METHOD="post" ACTION="villes.php">';
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
Is that correct ?
You've not got your select element wrapped in form tags which will prevent it being sent. What you actually want is this:
echo '<FORM METHOD="post" ACTION="villes.php">';
echo 'Make your choice : ';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
On the page "villes.php", you should then be able to refer to the variable $_POST["ville"] which will contain the selected option in the select.
echo $_POST["ville"];
In order to transfer the values of $nomFichier and $index, you want to send them as hidden elements as part of the form, so add the lines:
echo "<input type='hidden' name='nomFichier' value='".$nomFichier."'>";
echo "<input type='hidden' name='index' value='".$index."'>";
Which will again, be able to be grabbed from the $_POST array.
Moving your select into the FORM element will allow the application to post the data on submit.
echo 'Make your choice : ';
echo '<FORM METHOD="post" ACTION="villes.php">';
echo "<select name='ville'>";
foreach($nomVille as $option){
echo "<option value='{$option}'>{$option}</option>";
}
echo "</select>";
echo '<INPUT TYPE="submit" VALUE="Display">';
echo '</FORM>';
From the villes.php page you then have data retrievable in the data that you can access like this:
if (isset($_POST['ville'])) {
$someVar = $_POST['ville'];
}
You'll find it worth your time to Google "PHP form tutorial" and seeing how things work from there.
I have this javascript that i want to run in php,
the code bellow is supposed to be submitted in a form and then then prints it
but when submitted the javascript doesn't execute, the output is simply
var text = document.getElementById(\'course1\').options[document.getElementById(\'course1\').selectedIndex].text; document.write(text);
this is the whole thing,
echo "<form name\"find\" action=\"postEnrolled.php\" method=\"get\" class=\"required\" onsubmit=\"return validate(this);\">";
echo "<table width=\"225\" border=\"0\" align=\"center\" >";
echo "<tr>";
echo "<td width=\"181\"><label>Course#1:</label> <select name=\"dep1\" style=\"width:190px\" class=\"dep1\">";
echo "<option selected=\"selected\" value=\"\">Select Department</option>";
include('db.php');
$sql=mysql_query("select id,data from data where weight='1'");
while($row=mysql_fetch_array($sql))
{
$id = $row['id'];
$data = $row['data'];
echo '<option value="'.$id.'">'.$data.'</option>';
}
echo "</select><br/></td>";
echo "<td width=\"267\">";
echo "<label> </label><select name=\"course1\" class=\"course1\" style=\"width:200px\">";
echo "<option selected=\"selected\" value=\"\">Select Course</option>";
echo "</select>";
echo "<input type=\"hidden\" name=\"course_1\" value=\"
<script language='javascript' >
var text = document.getElementById('course1').options[document.getElementById('course1').selectedIndex].text;
document.write(text);
</script>
Am I missing something?
what I really want is to submit the text in the options and not the value of the options.
getElementById will only find an element whose id is course_1, not the name.
Don't put the script element inside the input element
You must have the DOM ready when calling it (use document.onload=function(){...yourcodehere...};)
At first sight, there is no PHP really involved in this problem. But are you aware that the code, as it is, wouldn't be executed when you change the value of the option ? If that's what you need, use onchange="yourcodehere;". But as it is an hidden field, maybe you should describe what you really want to achieve.
EDIT :
If what you want is change the hidden input when the user selects another option, here's how you can do it :
<input type=hidden name=course_1 id=course_1>
<select onchange="document.getElementById('course_1').value=this.options[this.selectedIndex].text;" ...
Your problem is that you're putting a <script> tag inside of the value attribute of the <input> tag. That isn't valid HTML or JavaScript and will not work.
why you don't post what is the actual error you got? actually this approach (including the javascript code into php tags) is not good at all.if you want to use javascript on any page, you just have to put it on the very top of your page under the script tags.
try it, will help u ..!
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());
$ -> #