0

Let's say I have this PHP variables :

$SelectedCountry = "USA";
$SelectedState = "Texas";

on the other hand, I have this javascript function to display all available countries and states :

function print_country(country_id){
    // given the id of the <select> tag as function argument, it inserts <option> tags
    var option_str = document.getElementById(country_id);
    option_str.length=0;
    option_str.options[0] = new Option('Where do you live now?','');
    option_str.selectedIndex = 0;
    for (var i=0; i<country_arr.length; i++) {
        option_str.options[option_str.length] = new Option(country_arr[i],country_arr[i]);
    }
}

function print_state(state_id, state_index){
    var option_str = document.getElementById(state_id);
    option_str.length=0;    // Fixed by Julian Woods
    option_str.options[0] = new Option('Select state','');
    option_str.selectedIndex = 0;
    var state_arr = s_a[state_index].split("|");
    for (var i=0; i<state_arr.length; i++) {
        option_str.options[option_str.length] = new Option(state_arr[i],state_arr[i]);
    }
}

my question is... how to make 'USA' and 'Texas' becomes selected <option> which generated by those two javascript functions? thanks.

NOTE #1 : you can see the complete code of javascript here : http://sourceforge.net/projects/countries/files/

NOTE #2 : those function called by adding this line on my PHP :

<script type="text/javascript" src="scripts/countries.js"></script>
<script language="javascript">print_country("country");</script>

so basically I need your help how to pass that PHP variables so that it can be 'received' by javascript function INSIDE that countries.js file.

2 Answers 2

1

One way is to just echo out some JavaScript statements:

<script>
<?php
    echo "
          var SelectedCountry = '$SelectedCountry';
          var SelectedState = '$SelectedState';
    ";
?>
</script>

Then just use them in your loops to check if the option needs to be selected or not.

If you're going to be doing a lot of this sort of thing, though, embedding PHP into JavaScript isn't really the best approach. Read up on AJAX and PHP's json_encode() function.

Sign up to request clarification or add additional context in comments.

Comments

1

There are two answers:

1 Use AJAX cal and pass back JSON

$.ajax({
    url: '/myScript.php',
    success: function(data) {
        //Do something
    }
});

myScript.php

return json_encode($myVar);

2 Embed PHP into the JavaScript

<script>
    var myPHPVariable = <?php echo $myVar; ?>
</script>

3 Comments

bro, I'm interesting with your second answer. because I don't do AJAX or JSON. but, those javascript function is not mine. I have no idea how to modify that javascript so it can recognize PHP variables and display a selected country as expected. I really appreciate if you can give me example of javascript modification. thanks bro.
So to be able to embed the PHP variable your javascript should be in a PHP file for example in index.php wrap it with <script language="javascript">//Your code here</script> and than use the PHP vars inside.
bro, what I mean is this... that two javascript functions : function print_country(country_id) and function print_state(state_id, state_index) are functions to generate <option> right? What I don't know is how to modify that javascript so that it can generate ONE <option selected>. that's all..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.