Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript and PHP functions

Is it possible to call a function from PHP using onsubmit from JavaScript? If so could someone give me an example of how it would be done?

function addOrder(){
    $con = mysql_connect("localhost", "146687", "password");
    if(!$con){
        die('Could not connect: ' . mysql_error())
    }

    $sql = "INSERT INTO orders ((user, 1st row, 2nd row, 3rd row, 4th row)
    VALUES ($session->username,1st variable, 2nd variable, 3rd variable, 4th variable))";

    mysql_query($sql,$con)
    if(mysql_query($sql,$con)){
        echo "Your order has been added";
    }else{
        echo "There was an error adding your order to the databse: " . mysql_error();
    }
}

That's the function I am wanting to call. Its an ordering system, you type in how much of each item you want, hit submit and it should add the order to the table.

like image 825
Shoaulin Avatar asked Oct 21 '08 10:10

Shoaulin


People also ask

Can I use PHP function in JavaScript?

You can call PHP function through Javascript by passing the value, you need as output of PHP function as a string in Javascript.

What is difference between JavaScript and PHP?

Javascript requires an environment for accessing the database. PHP allows easy and direct access to the database. JavaScript is used to create real-time games and applications, mobile applications etc. A PHP program is used to create dynamic pages, send cookies, receive cookies, collect form data, etc.

Can PHP and JavaScript interact?

Answer : PHP and Javascript cannot directly interact since PHP is a server side language and Javascript is a client-side language. However, we can exchange variables since PHP can generate Javascript code to be executed by the browser and it is possible to pass specific variables back to PHP via the URL.


1 Answers

You can not call a PHP function from Javascript...

Javascript is a client language (it's executed on the Web browser, after receiving the web page) while PHP is on the server side (it's executed before the web page is rendered). You have no way to make one call another.

...but you can get the result of an external PHP script

There is a Javascript function called xhttprequest that allows you to call any script on the Web server and get its answer. So to solve your problem, you can create a PHP script that outputs some text (or XML, or JSON), then call it, and you analyze the answer with Javascript.

This process is what we call AJAX, and it's far easier to do it with a good tool than yourself. Have a look to JQuery, it's powerful yet easy to use Javascript library that has built-in AJAX helpers.

An example with JQuery (client side) :

$.ajax({
   type: "POST", // the request type. You most likely going to use POST
   url: "your_php_script.php", // the script path on the server side
   data: "name=John&location=Boston", // here you put you http param you want to be able to retrieve in $_POST 
   success: function(msg) {
     alert( "Data Saved: " + msg ); // what you do once the request is completed
   }
like image 197
e-satis Avatar answered Sep 20 '22 16:09

e-satis