How to mix javascript and php?

Discussion in 'JavaScript' started by aayybb, Mar 9, 2010.

  1. #1
    Hi,

    I am trying to have a javascript function to mix with php inside of it. But I can't get the syntax right for the line of code with the mysql_query. How do we use the variable created in javascript in php? Any tip to fix this will be highly appreciated.

    <script type="text/javascript">
    function insertname()
    {
    var name ='Jane Smith';
    <?php

    mysql_query("INSERT INTO students (student_name) VALUES ('".?>name<?php ""."')");

    ?>
    }
    </script>
     
    aayybb, Mar 9, 2010 IP
  2. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You cannot mix Javascript and PHP. To do what you want you need to do an ajax request which will call a PHP Script where you can run your mysql query. Just search on google for jquery ajax or read this article which will give you a detailed how to on how to use AJAX: http://articles.sitepoint.com/article/ajax-jquery
     
    Nyu, Mar 9, 2010 IP
  3. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nyu, thank you very very much for the tip. I just read through the tutorial of jquery at w3schools.com.( I always start learning new things there and plan to go to the link in your response later).

    One more question here.
    What if I already have something like the following and it has some value in it through ajax already before the mysql part. Can I use it with the php mysql code? If so, how? Thanks for any help in advance.
    ....document.getElementById("name").innerHTML....
     
    aayybb, Mar 9, 2010 IP
  4. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not sure if i understand your question correctly... So you want the inner html value fom an element passed to your mysql query? Then you would write something like this (I assume that the insertname function will start the whole ajax request):

    The javascript code
    
    <script type="text/javascript">
    function insertname(id) {
      // will get the inner html from the element with the id specified in the variable id
      var name = $('#' + id).html();
    
      // submit ajax request
      $.post("test.php", { name: name },
        function(data) {
          alert("Status: " + data.status);
        }, "json");
    }
    </script> 
    
    HTML:
    Then your test.php would have the following code:
    
    <?php
    // establish a connection to your database here...
    
    // insert name into db
    mysql_query("INSERT INTO students (student_name) VALUES (".$_POST['name'].")");
    echo json_encode(array("status"=>"ok"));
    ?>
    
    PHP:
    Then somewhere in your code you have to call the insertname function with the id of the element you want to use the inner html from. It will do an ajax request ($.post) calling the php script specefied in the first parameter ("test.php") with the post parameters given in the second parameter ({ name: name } Note: Syntax is variableName: value. So if you would write { myname: "John" } in the PHP script you could acces this value with $_POST['myname']). After the ajax request is complete the function from the third parameter will be called. Just a simple alert in this case

    You can get more information about the jquery methods at http://docs.jquery.com/
     
    Nyu, Mar 10, 2010 IP
  5. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nyu, thank you very much for your response. Before I confuse myself with all the excellent jquery code your provided nicely, (will learn more about jquery soon)
    please allow me to explain my intention.
    I already had several functions doing the ajax thing to have a value in the innerHTML for "name".
    I am wondering if I can do something like this, (of course there is some error here)
    mysql_query("INSERT INTO students (student_name) VALUES ('".?>document.getElementById("name").innerHTML<?php ""."')");

    Thanks for any help in advance.
     
    aayybb, Mar 10, 2010 IP
  6. bhoo-day

    bhoo-day Greenhorn

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    you have to use AJAX to achieve it. You can try JQuery and Dojo. Both of those libraries provide good and easy Ajax method.

    you can't insert javascript variabel into PHP function.
     
    bhoo-day, Mar 10, 2010 IP
  7. Nyu

    Nyu Peon

    Messages:
    79
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    my above code snippets exactly does what you want ;) You just have to pass the id of the element you want to use the inner html from to the function insertname i wrote in my above post. For your example with the id "name" it would look like this:
    insertname("name");
    Code (markup):
    Then this Javascript function will search for the elemente with the id "name", get its inner html value and fire an ajax request that calls the php script named test.php. Within the PHP Script you can then do your mysql query. And as you passed the inner html value to the php script as the third paramater in the jquery post method (see the part with $.post in my post above) you can use it in the PHP script via the variable $_POST['name'] ;)
     
    Nyu, Mar 11, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    If you do not want to use AJAX, you can set the value of a hidden field of a form and submit the form with the action set to the same page, then get the var via $_POST
     
    Imozeb, Mar 11, 2010 IP
  9. weathor

    weathor Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I dont use post.. instead:

    function submitform(iso_country)
    {
    var formm = document.forms["myform"].elements["city"].value;var iso_c=iso_country;
    document.location.replace("i.php?f="+formm+"&c="+iso_c);
    }</script>';

    <form name="myform" action="javascript: submitform('FR')">
    city: <input type="text" name="city" />
    <a href="javascript: submitform('FR')">Search</a>

    </form>
     
    weathor, Mar 12, 2010 IP
  10. TheCatyl

    TheCatyl Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Remember google is your best buddy on internet, gives you thousends of answers 100x faster ;)
     
    TheCatyl, Mar 13, 2010 IP
  11. aayybb

    aayybb Peon

    Messages:
    128
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks for all the help. I will try all. Some reading needs to be done too:)
     
    aayybb, Mar 14, 2010 IP