Javascript函数后和调用PHP脚本 [英] Javascript function post and call php script

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

问题描述

在html中,我有几个按钮,这些按钮是针对特定状态的数据库中的每个对象自动创建的。每个按钮都有自己的ID。

  echo'< Button id =button'。$ counter。'onClick = clickedbutton('。$ counter。','。$ row ['OrderID']。')>'。< center>。$ row ['OrderID']。< br>。< ; /中心>中 '< /按钮>'。 

该按钮调用javascript函数clickedbutton并为其提供按钮的数量和该按钮的orderid 。

 函数clickedbutton(buttonid,orderid){
buttonid =button+ buttonid;




$ b $ p
$ b

这个函数加载按钮的数量并使它button0,button1等。orderid也成功通过。现在在我想调用外部php脚本的函数中,还必须将orderid传递给脚本。

 < ?php 
//连接数据库
include_once('mysql_connect.php');

//选择数据库
mysql_select_db(test)或die(mysql_error());

// SQL查询
$ strSQL =更新订单集OrderStatus ='进行中'其中OrderID ='+ orderid +';

mysql_close();
?>

我知道mysqli保护和所有,我会稍后调整。现在我想关注上面的问题,如何调用并通过变量orderid传递给phpscript。 解决方案

编辑2018年



是的,我还活着。您可以使用 fetch API而不是 jQuery 。它被广泛支持,除了(猜测谁?......)IE 11及更低版本,还有一个补丁。享受现代编码。



支持获取API



老解答



您会必须使用AJAX。

Javascript本身无法达到php脚本。您将不得不提出请求,将该变量传递给PHP,对其进行评估并返回结果。如果您使用jQuery发送 ajax ,请求相当简单:

  $。ajax({
data:'orderid ='+ your_order_id,
url:'url_where_php_is_located.php',
方法:'POST',//或GET
成功:函数(msg){
alert(msg);
}
});

并且您的php脚本应该获得如下订单ID:

  echo $ _POST ['orderid']; 

输出将作为字符串返回到成功函数中。

编辑



您还可以使用简写功能:

< pre $ $。get('target_url',{key:'value1',key2:'value2'})。
});

//或最终$ .post而不是$ .get


In html I have several buttons which are automatically made for each object in the database with a particular status. Each button gets its own id.

echo '<Button id="button'.$counter.'" onClick="clickedbutton('.$counter.', '.$row['OrderID'].')" >'."<center>".$row['OrderID']."<br>"."</center>".'</Button>';

The button calls the javascript function clickedbutton and gives it the number of the button and the orderid of that button.

function clickedbutton(buttonid,orderid){
buttonid = "button" + buttonid;

}

This function loads in the number of the button and makes it button0, button1 etc. The orderid is also succesfully passed through. Now in the function I want to call an external php script, but also orderid must be passed through to the script.

<?php
    //connect to database
    include_once('mysql_connect.php');

    // Select database
    mysql_select_db("test") or die(mysql_error());

    // SQL query
    $strSQL = "update orders set OrderStatus = 'In Progress' where OrderID = '" + orderid + "'";

    mysql_close();
?>

I know about the mysqli protection and all, I will adjust that later. Now I want to focus on the question above, how to call and pass through the variable orderid to the phpscript.

解决方案

EDIT 2018

Yeah, I am still alive. You can use the fetch API instead of jQuery. It is widely supported except (guess who?...) IE 11 and below but there is a polyfill for that. Enjoy modern coding.

Support for fetch API

OLD ANSWER

You will have to use AJAX.

Javascript alone cannot reach a php script. You will have to make a request, pass the variable to PHP, evaluate it and return a result. If you'are using jQuery sending an ajax request is fairly simple:

$.ajax({
    data: 'orderid=' + your_order_id,
    url: 'url_where_php_is_located.php',
    method: 'POST', // or GET
    success: function(msg) {
        alert(msg);
    }
});

and your php script should get the order id like:

echo $_POST['orderid'];

The output will return as a string to the success function.

EDIT

You can also use the shorthand functions:

$.get('target_url', { key: 'value1', key2: 'value2' }).done(function(data) {
    alert(data);
});

// or eventually $.post instead of $.get

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

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