Results 1 to 2 of 2

Thread: how javascript variable value can post in php

  1. #1
    Join Date
    Feb 2013
    Posts
    49
    Thanks
    25
    Thanked 1 Time in 1 Post

    Default how javascript variable value can post in php

    Hi,
    I am trying to make one combo list to another combo list through database. Here is what I did so far:

    <script>
    function run() {
    var str = document.getElementById("Combo1").value;
    document.getElementById("text1").value = str;
    }
    </script>

    <form name="testForm" id="testForm" method="POST" >
    <select name="Combo1" id="Combo1" onchange="run()">
    <option value="" selected>Select a country</option>
    <?php
    $link = mysql_connect("Domain_Name","DB_User","DB_User_Pwd");
    if (!$link) { die('Could not connect to Database: ' . mysql_error());
    }
    mysql_select_db("DB_Name",$link);
    $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>
    <?php
    } ?>
    </select>

    &nbsp;&nbsp;&nbsp; <select name="Combo2" id="Combo2">
    <option selected>Select a State</option>
    <?php
    $CName=$_GET['str'];
    $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>
    <?php
    } ?>
    </select>

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

    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.


    Raihan

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.

Similar Threads

  1. Replies: 7
    Last Post: 06-13-2011, 08:18 AM
  2. how to call a variable in javascript
    By sibelius in forum JavaScript
    Replies: 8
    Last Post: 01-23-2011, 03:35 PM
  3. Using Javascript variable in src tag?
    By HooliganR6 in forum JavaScript
    Replies: 2
    Last Post: 10-18-2006, 07:45 PM
  4. ASP variable with javascript
    By joecool2005 in forum JavaScript
    Replies: 0
    Last Post: 05-25-2006, 07:06 PM
  5. PHP variable in JavaScript
    By Jon Weir in forum PHP
    Replies: 0
    Last Post: 03-30-2006, 12:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •