PHP in Javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarayu
    New Member
    • Jul 2008
    • 15

    PHP in Javascript

    I used the php code in javascript function.When i click a button the javascript function is called and in that function i wrote the php code for insert values in database.With out submit the button the values are inserted into database.How can i prevent the inserting values without submitting the button?
    Here is my script :
    Code:
    <script>
    function insfun()
    {
    <?php
    $query=mysql_query("select * from tbl_products");
    while($row=mysql_fetch_array($query))
    {
    $queryins=mysql_query("insert into tbl_rateit(email,uid,action,item) values ('$_SESSION[email]','$_SESSION[uid]','buy','$row[itemid]')");
    }
    ?>
    }
    </script>
    <form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="image" src="../x-click-but01.gif" name="submit" onclick="return insfun();">
    </form>
    Last edited by Markus; Apr 3 '09, 12:01 PM. Reason: Added [code] tags.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    PHP code is executed at the server before the page is sent to the browser, thus your javascript function will be empty (check HTML source code). to run PHP code through JavaScript either submit the form (and let the server do the PHP) and wait for the results or use AJAX.

    Comment

    Working...