Javascript 和 PHP 函数 [英] Javascript and PHP functions

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

问题描述

是否可以使用 JavaScript 中的 onsubmit 从 PHP 调用函数?如果是这样,有人能给我一个如何完成的例子吗?

function addOrder(){$con = mysql_connect("localhost", "146687", "密码");如果(!$con){死('无法连接:'.mysql_error())}$sql = "插入订单((用户,第一行,第二行,第三行,第四行)VALUES ($session->用户名,第一个变量,第二个变量,第三个变量,第四个变量))";mysql_query($sql,$con)如果(mysql_query($sql,$con)){echo "您的订单已添加";}别的{echo "将您的订单添加到数据库时出错:".mysql_error();}}

这就是我想要调用的函数.它是一个订购系统,您输入您想要的每件商品的数量,点击提交,然后它应该将订单添加到表格中.

解决方案

你不能从 Javascript 调用 PHP 函数...

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

...但是你可以得到一个外部 PHP 脚本的结果

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

这个过程就是我们所说的AJAX,用一个好的工具比你自己.看看 JQuery,它功能强大且易于使用,内置 AJAX 帮助程序.>

JQuery 示例(客户端):

$.ajax({type: "POST",//请求类型.您最有可能使用 POSTurl: "your_php_script.php",//服务端的脚本路径data: "name=John&location=Boston",//这里你放了你希望能够在 $_POST 中检索的 http 参数成功:功能(味精){alert("数据保存:" + msg );//请求完成后的操作}

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.

解决方案

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
   }

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

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