简体   繁体   English

在按钮单击中调用php函数

[英]call php function in button click

I need to call the PHP function when I click the button in html page. 单击html页面中的按钮时,需要调用PHP函数。

<script>
 $(document).ready(function(){
    $('#submit').click(function() {

    $.ajax({
    type: "POST",
    url: "hello.php",
    }).done(function(  ) {
    alert( "Data Saved: ");
    });    
});

});
</script>

<input type="submit" name="submit"  id="submit" value="Buy!" />

Hello.php: Hello.php:

   <?php
   if(isset($_REQUEST['submit']))
   {
      counterminus();
   }
   function  counterminus()
   {
      echo "method called";
   }

I have tried the above method but its not working properly.I need to print the string in the counterminus() method when i click the button. 我已经尝试了上述方法,但是无法正常工作。当我单击按钮时,需要在counterminus()方法中打印字符串。

<script>
$(document).ready(function(){
$('#submit').click(function() {

 var val = $('#submit').val();
$.ajax({
type: "POST",
data:"var="+val,
url: "hello.php",
}).done(function(  ) {
alert( "Data Saved: ");
});    

}); });

}); });

the above script will POST the variable name var with the value of the submit to hello.php 上面的脚本将POST变量名var与提交到hello.php的值

on hello.php 在hello.php

 echo $_POST['var'];

will result in "buy!" 将导致“购买!”

so 所以

  if(isset($_POST['var'])){
  .....
  .....

Everything looks fine to me, except that you do not print the data returned by your AJAX call, which shall be provided as a parameter to your done callback. 在我看来,一切都很好,但是您不打印AJAX调用返回的数据,该数据应作为已done回调的参数提供。

Try this: 尝试这个:

$.ajax({
    type: "POST",
    url: "hello.php",
}).done(function(data) {
    alert( "Data Saved: " + data);
});    

if by any case you put the input type="submit" button inside a form tag. 如果您将输入type =“ submit”按钮放置在表单标签中, you should stop the event from executing.. put this at your ajax call. 您应该停止执行事件。将其放在您的ajax调用中。 Because the form will post before the ajax call completes. 因为该表单将在ajax调用完成之前发布。

<script>
$(document).ready(function(){
      $('#submit').click(function(e) {
               e.preventDefault();
               $.ajax({
                     type: "POST",
                     url: "hello.php",
                      }).done(function(  ) {
                             alert( "Data Saved: ");
                                });    
         });

         });
</script>

pass a variable submit with url 通过url传递变量

try the following code 试试下面的代码

$.ajax({
        type: "POST",
        url: "1.php?submit=",
        }).done(function( data ) {
        alert( "Data Saved: "+data);
        }); 

or u can pass values as data 或者你可以将值作为数据传递

$.ajax({
            type: "POST",
            url: "1.php",
            data: {submit:""},
            }).done(function( data ) {
            alert( "Data Saved: "+data);
            }); 

Use exit in php. 在php中使用exit。 now it returns "method called" string 现在返回“方法调用”字符串

   if(isset($_REQUEST['submit']))
   {
      counterminus();
   }
   function  counterminus()
   {
      echo "method called";
      exit;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM