pass selectbox option value to php [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
<body>
<form method="post" action="eex4.php">
<label for="text_2">Select the Scheme: </label>
<select name="users" id="users">
<option value="">Select:</option>
<option value="1">Sem1</option>
<option value="2">Sem2</option>
<option value="3">Sem3</option>
<option value="4">Sem4</option>
<option value="5">Sem5</option>
<option value="6">Sem6</option>
</select>
<input type="submit" value="Submit" name="btn" onClick="ResultPage()"/>
</form>
</body>
The above code output has selectbox with submit button. when i click the submit button it call the script.
<script>
function ResultPage()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var select = document.getElementById('users');
var sem = select.options[select.selectedIndex].value;
var uurl = "eex4.php?q=" + sem;
xmlhttp.open("GET",uurl,true);
xmlhttp.send();
}
</script>
i need to pass a value of selectbox which option i selected and send to following php file.
eex4.php
<?php
$schm = $_GET['sem'];
$txt1 = "Semester";
echo $txt1 ." ". $schm;
?>
My expected result is:
"Semester[value of option have select]" but it simply displaying "Semester"
can any one find the error..

use
$schm = $_GET['q'];
because you are passing q here
var uurl = "eex4.php?q=" + sem;
The problem is with your form when you click the submit button it posts the data to eex4.php and just displays Semester.
Change
<input type="submit"
to
<input type="button"
So when you click this button the content will load in the page without refreshing the page.

The ajax part has been taken from w3schools. You just have to replace $schm = $_GET['sem']; to $schm = $_GET['q'];

change
var uurl = "eex4.php?q=" + sem;
to
var uurl = "eex4.php?sem=" + sem;
because you are getting 'sem' in eex4.php but passing 'q' to it

You are passing value with through q
$schm = $_GET['q'];

<body>
<div id="txtHint"></div>
<form method="post" action="eex4.php">
<label for="text_2">Select the Scheme: </label>
<select name="users" id="users">
<option value="">Select:</option>
<option value="1">Sem1</option>
<option value="2">Sem2</option>
<option value="3">Sem3</option>
<option value="4">Sem4</option>
<option value="5">Sem5</option>
<option value="6">Sem6</option>
</select>
<input type="submit" value="Submit" name="btn" onClick="return ResultPage()"/>
</form>
</body>
<script>
function ResultPage()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
var select = document.getElementById('users');
var sem = select.options[select.selectedIndex].value;
var uurl = "eex4.php?q=" + sem;
xmlhttp.open("GET",uurl,true);
xmlhttp.send();
return false;
}
</script>
eex4.php
<?php
$schm = $_GET['sem'];
$txt1 = "Semester";
echo $txt1 ." ". $schm;
?>

change
$schm = $_GET['sem'];
to
$schm = $_GET['q'];

Related

Call a PHP script when an option is selected from a select element in HTML

At the moment I have the following markup code on my website:
<select>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
I want to have a PHP script in the same directory as my webpage fire when I select a particular option. How would I go about doing this? Thank you.
call a ajax on change of select like this
<select onchange="javascript:callAjax(this.value);">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<script>
function callAjax(sel_opt)
{
var url = "your_php_file.php?select_option="+sel_opt;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var result = xmlhttp.responseText;
// put your result where ever you want
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
</script>
your_php_file.php
<?php
// your code
?>
Do an Ajax call. Here's an example. hope this helps to understand.
cheers!

PHP Ajax with a form

I have a form that let user to choose where them come from..
I have list out the country list by select box..
and i success to do it onchange and use ajax to read out all the state list..
actually i have two php file.. one is the main one that contain all the code include the form.. another one is generate state list.. my ajax code and form are in the main php
this is the one that generate state list...
$cID = $_GET["country"];
$sql = "SELECT * FROM States WHERE CountryID=$cID";
$result = $db->get_results($sql);
if (isset($result)) {
foreach ( $result as $states ) {
?>
<input type="checkbox" name="chkState[]" value="<?=$states->StateID?>" /> <?=$states->StateName?><br />
<?php
}
}
this is part of my code that read out..
but my problem is...
where i submit the form..
i only get the variable of country.. i cannot take the data of state that use ajax to generate out...
sample of my form
<form action="" name="place">
<select name="cboCountry" class="drop" onchange="showState(this.value)">
//some php code the list out the country
</select>
<div id="txtHint">
</div>
<input type="submit" name="submit" value="submit" />
</form>
and here my ajax code
<script>
function showState(str)
{
alert('xxx');
var xmlhttp;
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","state_list.php?country="+str,true);
xmlhttp.send();
}
</script>
so when i submit the from with a save button... i only get the date of country and state are missing.. any body can help me? or having any way to do what i want?

onsubmit form values to php sql request

I have many input values from a form, and I would like to push them to PHP code using ajax code. Here's an example of what i'm trying to do. I have test1 and test2 that i want to keep track when user press search button.
Right now it only get the value of test1 "getResults(this.test1.value)" I would like to know how to get the test2 value using the same method.
<form name="input" action="" method="" onsubmit="getResults(this.test1.value); return false;">
<input type="text" name="test1">
<select id="test2" name="test2">
<option value="">Make a choice ...</option>
<option value="c1">choice 1</option>
<option value="c2">choice 2</option>
<option value="c3">choice 3</option>
</select>
<input type="submit" value="Search">
</form>
<div id="here"><b></b></div>
The getresults method, right now it only support one string, how can I add more arguments ?
function getResults(str)
{
if (str=="")
{
document.getElementById("here").innerHTML="";
return;
}
else (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("here").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","info.php?q="+str,true);
xmlhttp.send();
}
and finaly and more importantly how can I retreive values from info.php ? Thanks a lot
<?php
$test1 = $_GET['test1'];
echo "$test1";
$test2 = $_POST["test2"];
echo "$test2";
?>
You don't need to pass argument to the function. We can grab the form fields value there.
<html>
<head>
<script type="text/javascript">
function getResults()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("here").innerHTML=xmlhttp.responseText;
}
}
var data = "test1="+document.getElementById("test1").value+"&test2="+document.getElementById("test2").value;
xmlhttp.open("POST","info.php",true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send(data);
}
</script>
</head>
<body>
<form name="input" action="" method="" onsubmit="getResults(); return false;">
<input type="text" name="test1" id="test1">
<select id="test2" name="test2">
<option value="">Make a choice ...</option>
<option value="c1">choice 1</option>
<option value="c2">choice 2</option>
<option value="c3">choice 3</option>
</select>
<input type="submit" value="Search">
</form>
<div id="here"><b></b></div>
</body>
</html>
Than in your info.php file do this to grab the variables sent via Ajax,
<?php
$test1 = $_POST["test1"];
$test2 = $_POST["test2"];
echo "Test1: ".$test1."<br/>Test2: ".$test2."";
?>
The reason it doesn't pull both values is because 1) you need to add a method to your form. You can either do post or get, not both.2) In your php you define test 1 as from the get array and variable 2 as from the post array. If you make $test2 from the get array it should work.

PHP - AJAX and MySQL

This is my code:
<?php
$q=$_GET["q"];
require("../db.php");
$res_prop_name = mysql_query("select * from property where prop_short='$q'");
$row_prop_name = mysql_fetch_assoc($res_prop_name);
echo $row_prop_name['prop_name'];
?>
<script type="text/javascript">
function showProperty(str)
{
if (str=="")
{
document.getElementById("prop_name").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("prop_name").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax/get-property-name.php?q="+str,true);
xmlhttp.send();
}
</script>
<select id="prop_short" name="prop_short" onchange="showProperty(this.value)">
<option value="">Select a property</option>
<option value="Property2">Property1</option>
<option value="Property2">Property2</option>
</select>
<input type="text" id="prop_name" name="prop_name" onChange="showProperty(this.value)">
The answer to my question must be very simple however I just can't make it work.
I understand that in the ajax script the innerHTML feeds the text into a div that has the id prop_name for example. I tried it and it worked. But how can I feed the returned text into a input type="text" field? I've tried innerText instead of HTML but it doesnt work.
Putting the comment as an answer: For input elements it's document.getElementById("xxx").value instead of .innerHTML.

Problem with <select> tag in php and ajax

I have this code (below) that works on FF&Chrome but doesn't work on IE*. I am trying to populate the select tag with email address. Thanks
question.php
<html>
<head>
<script type="text/javascript">
function myFunction(val)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","qsrcipt.php?q="+val,true);
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send();
}
</script>
</head>
<body onload="myFunction(document.myForm.mySelect.value)">
<form name="myForm" style="float:left; margin-right: 10px; overflow: hidden;">
<select name="mySelect" onchange="myFunction(this.value)" size="10">
<option value="1" selected="selected">level 1</option>
<option value="2">level 2</option>
<option value="3">level 3</option>
</select>
</form>
<div>
<select id="myDiv" size="10"></select>
</div>
</body>
</html>
qsrcipt.php
<?php
// Fill up array with names
$lsts = array("Anna","Brittany","Cinderella","Diana","Eva","Fiona");
//get the q parameter from URL
$q=$_GET['q'];
if ($q == '1')
{
foreach($lsts as $lst){
echo "<option id='1'>".$lst."</option>";
}
}
else
{
echo "<option id='1'>this is a test</option>";
}
?>
Your $lsts contains the array of names not email.
Secondly, instead generating the HTML server side, it's better to return the values of $lsts as a JSON encoded array and, in JavaScript, dynamically generate the select list.

Categories