problem in reading url variable?

Discussion in 'PHP' started by cutemicky, Mar 18, 2008.

  1. #1
    hi
    I am using php + java script + ajax for my project development i am passing one variable from one page to another using java script
    document.location("somepage.php?id="+56);
    i am storing it in
    $getid=$_REQUEST['id']; on the other page its working fine when i am writting this code outside any function but once when i call it in any function that ajax is calling its giving an error id is not defined.
    plz help me out i am really feeling myself helpless in this code
    than u
     
    cutemicky, Mar 18, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    hi

    are you trying something like this

    <?php

    $connection = mysql_connect('localhost', 'username', 'password');

    $sql = mysql_select_db('db1', $connection);

    $userid = "Snib";

    $query = "SELECT * FROM `table1` WHERE userid='$userid'";

    $result = mysql_query($query);

    $data = mysql_fetch_assoc($result);

    $usernum = $data['usernum'];

    ?>



    <script language="javascript" type="text/javascript">

    var userid = "<?php echo $userid; ?>";

    var usernum = "<?php echo $usernum; ?>";

    //if usernum is less than or equal to 50
    if (usernum <= 50) {

    alert("You are not allowed on this page, " + userid);
    setTimeout('window.location = "otherpage.php"', 3000); // user leaves in 3 seconds

    } else {

    alert('Welcome to this page, ' + userid);

    }

    </script>

    <?php mysql_close($connection); ?>

    Try this code
    Regards

    Alex
     
    kmap, Mar 18, 2008 IP
  3. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Ajax only calls javascript functions which are client side. If you need to access the variable, you must write it and asign it to a javascript variable. If you use ajax to load another php page behind the scenes, you'll need to return the variable as part of the response text
     
    NoamBarz, Mar 18, 2008 IP