Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


javascript and php functions


javascript and php functions  using -'php,javascript,function'

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.
    

asked Oct 19, 2015 by vijayshukla80
0 votes
5 views



Related Hot Questions



Government Jobs Opening

And then have a PHP file named add_order.php with something like:

$success = 0;
$con = mysql_connect("localhost", "146687", "password");
if(!$con) {
    $message = 'Could not connect to the database.';
} else {
    // SANITIZE DATA BEFORE INSERTING INTO DATABASE
    // LOOK INTO MYSQL_REAL_ESCAPE_STRING AT LEAST,
    // PREFERABLY INTO PREPARED STATEMENTS
    $query_ok = mysql_query("INSERT INTO `orders` ....");
    if($query_ok) {
        $success = 1;
        $message = "Order added.";
    } else {
        $message = "Unable to save information";
    }
}

print json_encode(array('success' => $success, 'message' => $message));

For this beauty to work you will have to go to the jQuery website, download it, and include the jquery.js file.

I haven't tested the above but it should work. Good luck.

answered Oct 19, 2015 by ajit.chavhan
0 votes

Yes, another great and easy tutorial for learning ajax is:

http://24ways.org/2005/easy-ajax-with-prototype/

Prototype is another thing I recommend if you havent gone to far with your project and need to revert a lot of functionality.

http://www.prototypejs.org/

Good luck!

answered Oct 19, 2015 by pradip.bhoge
0 votes

Sorry, your question is too basic. Depending on what PHP function you want to call on formsubmit Ajax will not help you. Are you aware of the fundamental (clientside/serverside) differences between PHP and JavaScript?

answered Oct 19, 2015 by mca.agarwal

...