SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post Is this how to put parameter ?

    Hi, can you help me please is this correct to put parameter in window.location ?,because my code did not work.

    PHP Code:
     <?php

        
    if(isset($_post['idno'])){
           
    $id $_post['idno'];
          
    $age $_post['age'];
      }

    ?>

    <script type="text/javascript">
        var idno = '<?php echo $id;?>';
        var myage =  '<?php echo $age;?>';
       

            window.location.href=('myinformation.php?id='+idno+'&age='+myage);

    </script>

    Thank you in advance.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Your current code won't work because the PHP tags are within quotes which the PHP compiler can't see, however the browser will see this as a string. See the below which is one way to output PHP variables into JavaScript code however a much more common method would be to use a JSON array.

    Plain JavaScript
    Code JavaScript:
    var idno  = <?php echo "'$id'"; ?>,
        myage = <?php echo "'$age'"; ?>;
     
    window.location.href = 'myinformation.php?id=' + idno + '&age=' + myage;

    Using a JSON array (recommended)
    PHP Code:
    <?php
        
    $data 
    = array(
        
    'idno' => (isset($_POST['idno']) && $_POST['idno']) || '',
        
    'age'  => (isset($_POST['age']) && $_POST['age'])   || ''
    );

    ?> 
    <script type="text/javascript"> 
        var data = <?php json_encode($data); ?>;
        window.location.href = 'myinformation.php?id=' + data.idno + '&age=' + data.myage;
    </script>

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,146
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi chris.upjohn, wow thank you for the solution it's working now...and also i learn that it is okay to put isset in array and pass to json.

    Thank you so much.


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
  •