View Full Version : how javascript variable value can post in php

04-26-2013, 09:22 AM
I am trying to make one combo list to another combo list through database. Here is what I did so far:

function run() {
var str = document.getElementById("Combo1").value;
document.getElementById("text1").value = str;

<form name="testForm" id="testForm" method="POST" >
<select name="Combo1" id="Combo1" onchange="run()">
<option value="" selected>Select a country</option>
$link = mysql_connect("Domain_Name","DB_User","DB_User_Pwd");
if (!$link) { die('Could not connect to Database: ' . mysql_error());
$category = "SELECT Country_Name FROM Test_Country";
$query_result = mysql_query($category);
while($result = mysql_fetch_assoc($query_result)) {
<option value = "<?php echo $result['Country_Name']?>"><?php echo $result['Country_Name']?></option>
} ?>

&nbsp;&nbsp;&nbsp; <select name="Combo2" id="Combo2">
<option selected>Select a State</option>
$category = "SELECT State_Name FROM Test_State LEFT JOIN Test_Country USING (CountryID) WHERE Country_Name='$CName'";
$query_result = mysql_query($category); while($result = mysql_fetch_assoc($query_result)) {
<option value = "<?php echo $result['State_Name']?>"><?php echo $result['State_Name']?></option>
} ?>

&nbsp;&nbsp;&nbsp;TextBox1:&nbsp;&nbsp; <input type="text" id="text1" placeholder="get value on option select"><br>

Please have a check on my site: raihans.co.uk, User: dbuser, passwd. Hover to UserLog In/Out, then click on DB Test. I have left a sample, as I need. I have used the data directly into code in the sample.

In second list (Combo2), if I enter Country_Name directly into query, it gives the state name. But I want like, if I select Contry_name from 1st list, then it will give State_name into second list. Just as the sample. How I may do this, please reply. I am strugling with that for long time.

I am not sure, but I think, if I can pass the value in 'str' from script to php, it may possible. Don't know how.

Thanking you in advance.


04-26-2013, 02:19 PM
PHP happens *first*, on your server.

javascript runs afterwards, on the user's computer. PHP is long gone. javascript never even knows about PHP.

So, in short, no. What you can do is use AJAX to send the value of str back to your server, where php can receive it, query the DB, and send the results back to the page.