SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2001
    0 Post(s)
    0 Thread(s)

    Question Global variable?

    I'm able to retrieve data from the database but I'm having trouble using the returned variable. At the bottom of the code example I inserted DISPLAY JAVASCRIPT VAR region here??? where I need to use the region variable. How can I change my code to do that? Here's my code:

    Code JavaScript:
         function handlePosition(pos)
    { $.ajax({                                      
          url: 'map.php',
          data: "lat=" + pos.coords.latitude + "&lon=" + pos.coords.longitude,
          dataType: 'json',
          success: function(data)
          { region = data[0]; } 
          <select id="cities" multiple="multiple" name="cities[]" title="Confirm Your Deal Location"  multiple="multiple" data-native-menu="false">
          <?php $sql = "SELECT DISTINCT name, id
    FROM `db` WHERE id != '1' ORDER BY name
      $sql_result = mysql_query($sql) or die(sprintf("Couldn't execute query, %s: %s", db_errno(), db_error()));
    while ($row = mysql_fetch_array($sql_result)) {
     $mailregion = $row['name'];
     $group_id = $row['id']; ?>
          <option value="<?php echo "$group_id"; ?>" <?php if (($mailregion == 'Internet') || ($mailregion == DISPLAY JAVASCRIPT VAR region here???)) { echo "selected=\"selected\""; } ?>><?php echo "$mailregion"; ?></option>

    Thanks in advance,

    Five Star Review Script - Add reviews to your website!
    Mixed Martial Arts - Learn the fastest growing martial art
    Women's Self-Defense - Courses and DVDs available

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Christchurch, New Zealand
    104 Post(s)
    4 Thread(s)
    Quote Originally Posted by bigtime View Post
    I'm able to retrieve data from the database but I'm having trouble using the returned variable.
    • PHP runs on the server-side
    • The web page is created and sent to the client
    • The client browser interprets the page
    • JavaScript then runs on the client

    It is not possible for JavaScript to be run at the same time as PHP, so PHP cannot directly get any JavaScript information.

    What PHP needs to do is to set the selected option without knowing about the JavaScript variable.

    Code php:
    <?php if (($mailregion == 'Internet')) { echo "selected=\"selected\""; } ?>

    When JavaScript runs and does its job, you can then use JavaScript to update the selected option depending on what it now knows.

    Something like this should work:

    Code javascript:
    var region = data[0];
        $('#cities option:contains(' + region + ')')
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Posting Permissions

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