CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2004
    Posts
    187

    Angry Javascript + Php + Javascript

    Here is my code:

    Code:
    <script language="javascript">
    function show_list(){
    var val = document.myform.master.options[document.myform.master.selectedIndex].value;
    
      if(val==19){ 
      <?
      $sql = 'select * from table where id=19 ORDER BY id ASC'; 
      $res = mysql_query($sql);
      $tot = mysql_num_rows($res);
      for($i=0;$i<$tot;$i++){
        $r = mysql_fetch_array($res);
      ?>
      document.myform2.master.options[<?=$i?>]=new Option("<?=$r[city]?>", <?=$r[id]?>, true, false)
      <? } ?>
      }
    }
    </script>
    The problem is that I´m setting the id value and I dont want to do that.
    How can I put the javascript value (val) into this php line below?
    sql = 'select * from table where id=19 ORDER BY id ASC';

    I´d like something like this:
    sql = 'select * from table where id='+val+' ORDER BY id ASC';

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript + Php + Javascript

    Remember that PHP is server-side and therefore is interpreted before your JavaScript. That means you have to create the JavaScript and then send it to the server.

    You can do this through 2 options, using a $_POST form or putting it in the url and using $_GET.

    PHP Code:
    <?php
    $id 
    = @$_GET['id'];

    $sql "SELECT * FROM `table` WHERE `id` = '$id' ORDER BY `id` ASC";
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2006
    Posts
    28

    Re: Javascript + Php + Javascript

    Quote Originally Posted by peejavery
    $id = @$_GET['id'];
    Whats with the @ symbol? If id isnt set, then $id will just become nothing.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript + Php + Javascript

    Quote Originally Posted by danbopes
    Whats with the @ symbol? If id isnt set, then $id will just become nothing.
    And will also cause an error to be written to the log. I use the @ to avoid excess logging of something I already know.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Sep 2006
    Posts
    28

    Re: Javascript + Php + Javascript

    Quote Originally Posted by peejavery
    And will also cause an error to be written to the log. I use the @ to avoid excess logging of something I already know.
    I never knew it logged empty variables...wait where? on the server(like the error_log file), cause I never seen it log any of this anywhere.

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: Javascript + Php + Javascript

    In the PHP folder on the server is file named error.log. All errors are written there. If you call $_GET['var']; and there is not [u]var=[/url] in the URL line, you will get a silent log error.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured