将执行 php 代码块的 JS OnClick 事件


JS OnClick event that will execute block of php codes

有没有办法在我点击一个按钮时它会执行一堆php代码?

例如;

<input type="button" value="Submit Order" class="minibutton" **onClick="executePHP"**>
这个

按钮对象就在这里,如果我点击它,这个php代码将被执行。

    include("../includes/connect.php");
    include("../includes/generateTimestamp.php");
    include("../includes/knowthename.php");

    $pid=$_POST['submit_id'];
    $pname = $_POST['submit_name'];
    $total = $_POST['submit_total'];
    $quantity = $_POST['submit_qty'];
    $price = $_POST['submit_price'];
    $unit = $_POST['submit_unit'];
    $change = $_POST['submit_sukli'];
    $payment = $_POST['submit_payment'];
    $count = $_POST['count'];
    //explode
    $newpid = explode(";",$pid);
    $newpname = explode(";",$pname);
    $newquantity = explode(";",$quantity);
    $newprice = explode(";",$price);
    $newpriceunit = explode(";",$unit);

    $sql = mysql_query("INSERT INTO sales (sales_date, total_price, emp_id) VALUES ('$timestamp','$total', '$employee_id')");
    mysql_query($sql);
    $id_gen = mysql_insert_id();
    $count = $count - 1;
    while($count = -1){
            $product_id = $newpid[$count];
            $product_name = $newpname[$count];
            $product_quantity = $newquantity[$count];
            $product_price = $newprice[$count];
            $sql2("INSERT INTO sales_details (sales_id,product_id,sales_qty,unit_price,net_price) VALUES ('$id_gen','$newpid[$count]','$product_quantity[$count]',
            '$product_price[$count]','$newpriceunit[$count]')");
            mysql_query($sql2);         
        }
    header('Location: prompt.php?x=5');
    }else{
        echo 'Nothing here!';
        }

我想使onclick函数成为一个断路器,如果我单击它,它将取消中断并继续执行这些代码,

如果我没有办法,还有其他办法吗?

函数 isset 不起作用,因为原始数据来自 $_POST,数据将丢失(数组)。我希望是点击事件,但我不知道如何。

请开导我电脑的智慧!

您只能将 JavaScript 附加到浏览器事件。如果要执行服务器端代码(如 PHP),则需要调用服务器。在你的情况下,忘记javascript,只是把你的表单发布到你的PHP页面。

<form method="post" action="file.php">
  <!-- form inputs -->
  <input type="button" value="Submit Order" class="minibutton" />
</form>

然后将代码放入file.php

编辑

将所有帖子数据放在隐藏的表单字段中。然后,当您单击该按钮时,它将重新提交帖子数据。

foreach($_POST as $key => $val) {
    echo '<input type="hidden" name="'.htmlspecialchars($key).'" value="'.htmlspecialchars($val).'" />'."'n";
}

如果你有一个多维的post数组,只需serialize()它,把它放在1个字段中

echo '<input type="hidden" name="data" value="'.htmlspecialchars(serialize($_POST)).'" />'."'n";

然后在另一边unserialize()它。

$_POST = unserialize($_POST['data']);

使用 jQuery:

$('.minibutton').on('click', function(e) {
    // do some ajax stuff here 
});

示例:http://api.jquery.com/jQuery.ajax/

如果您的.php需要来自单击元素的值,不,则不需要。如果你的代码本身就是一堆 php,与客户端请求无关,那么你可以在 js 函数中回显它并在单击时调用它。