Javascript和PHP功能 [英] Javascript and PHP functions

查看:121
本文介绍了Javascript和PHP功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用JavaScript从 onsubmit 从JavaScript调用函数?如果是的话,有人可以给我一个例子,说明它将如何完成?

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.

推荐答案

您无法从Javascript调用PHP函数...



Javascript是一种客户端语言(它在Web浏览器上执行,收到网页后),而PHP在服务器端(它是在网页呈现之前执行的)。您无法再打一个电话。

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.

有一个名为xhttprequest的Javascript函数,它允许您调用Web服务器上的任何脚本并获得答案。因此,要解决您的问题,您可以创建一个输出一些文本(或XML或JSON)的PHP脚本,然后调用它,然后使用Javascript分析答案。

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.

这个过程就是我们所说的 AJAX ,用一个好的工具做起来要容易得多你自己。看看 JQuery ,它功能强大但易于使用,内置AJAX助手的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.

JQuery示例(客户端):
$ b $

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
   }

这篇关于Javascript和PHP功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆