How do you call a PHP function from javascript

Discussion in 'PHP' started by Imozeb, Feb 6, 2010.

  1. #1
    I have created a html form, a javascript validation function, and a PHP insert into database function.
    I made the html form call the javascript validation function and if the form is valid then it should call the PHP insert function but that doesn't work. So how do I call a PHP function from javascript.

    Note: the PHP is above the header tag, the javascript is in the header tag, and the html form is in the body tag

    I tried something like this in javascript:

    <?PHP
    insertintodatabase();
    ?>


    or...

    <?PHP
    echo (insertintodatabase();)
    ?>


    neither worked... :( Can you set a variable in javascript and pass it into PHP? or can someone give me a working example in AJAX?

    Thanks in advance.

    ~Imozeb
     
    Last edited: Feb 6, 2010
    Imozeb, Feb 6, 2010 IP
  2. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #2
    You'll have to use Ajax.
     
    Bec0de, Feb 6, 2010 IP
  3. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's the mostly relevant search I could help you with: bytes.com/topic/javascript/answers/445757-onload-php
     
    ProtegeSales, Feb 6, 2010 IP
  4. nadiralishah_webexpert

    nadiralishah_webexpert Guest

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    i suggest you to go thru the AJAX (asynchronous javascript and xml) tutorial at w3schools.com to call php using javascript and get response..
     
    nadiralishah_webexpert, Feb 6, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I went through the AJAX tutorial on w3schools and couldn't find anything pertaining to my problem. Could you give me an example of how it would work?
     
    Imozeb, Feb 6, 2010 IP
  6. nadiralishah_webexpert

    nadiralishah_webexpert Guest

    Messages:
    229
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This must help you;

    <html>
    <body>

    <span id="header"></span>

    <script type="text/javascript">
    xmlhttp=null;
    if (window.XMLHttpRequest)
    {// code for Firefox, Mozilla, IE7, etc.
    xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
    xmlhttp.onreadystatechange = function()
    {
    if(xmlhttp.readyState == 4)
    {
    x=xmlhttp.responseText;
    document.getElementById("header").innerHTML=x;
    }
    }
    xmlhttp.open("GET", "urFile.php", true);
    xmlhttp.send(null);
    }
    else
    {
    alert("Your browser does not support XMLHTTP.");
    }
    </script>
    </body>
    </html>

    without the refresh of your page, this script will call the php file urFile.php. and response will come as text, whatever ufFile.php will need to print or display on webpage, that will come as response that you can use.. u just inlcude the file where ur php function exist that you need to call, include that file on urFile.php and call that function on urFile.php..

    should work..
     
    nadiralishah_webexpert, Feb 7, 2010 IP
  7. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks.

    ~Imozeb :)
     
    Imozeb, Feb 7, 2010 IP