Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择HTML格式的PHP函数_Php_Html_Ajax - Fatal编程技术网

选择HTML格式的PHP函数

选择HTML格式的PHP函数,php,html,ajax,Php,Html,Ajax,我刚读了这篇文章,我需要一些帮助 在这个例子中,HTML总是在PHP中调用相同的函数,“test_function”函数,但是我想根据HTML表单的值调用不同的函数 我试过了,但没有成功: <?php if (is_ajax()) { if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists $action = $data["favorit

我刚读了这篇文章,我需要一些帮助

在这个例子中,HTML总是在PHP中调用相同的函数,“test_function”函数,但是我想根据HTML表单的值调用不同的函数

我试过了,但没有成功:

<?php
if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $data["favorite_beverage"];
    switch($action) { //Switch case for value of action
      case "test": test_function(); 
      case "testin": testin_function();
      break;
    }
  }
}

//Function to check if the request is an AJAX request
function is_ajax() {
  return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}

function test_function(){
  $return = $_POST;

  //Do what you need to do with the info. The following are some examples.
  //if ($return["favorite_beverage"] == ""){
  //  $return["favorite_beverage"] = "Coke";
  //}
  //$return["favorite_restaurant"] = "McDonald's";

  $return["json"] = json_encode($return);
  echo json_encode($return);
}

function testin_function(){

  echo json_encode("You did it !");
}

?>
a)带断路开关和开关盒的开关基于$POST[“操作”]

阅读开关上的手册


正确使用
开关
您似乎没有初始化
$data
数组。您可能使用了错误的变量。请阅读开关手册,这不是OP代码的唯一错误。阅读他们关于使用开关的问题下的评论。每个开关都需要断开。开关没有问题,因为$data不存在。。没有$data,没有切换的案例。这是问题的“一部分”。请自行查看,OP在希望单独调用的两个函数调用之间没有中断。当然,这是问题的一部分,但我看到更多的焦点集中在“jQuery form.serialize()with other parameters”问题上。希望我的答案适合OP。让我们看看……谢谢大家,我只需要根据用户的输入选择不同的函数,然后你来解决它。
<?php
if (is_ajax()) {
  if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
    $action = $POST["action"];
    switch($action) { //Switch case for value of action
        case "test": 
            test_function(); 
            break;
        case "testin": 
            testin_function();
            break;
    }
  }
}
<script type="text/javascript">
$("document").ready(function(){
  $(".js-ajax-php-json").submit(function(){

    // serialize the form
    var data = $(this).serialize();

    // append action - set to the value of the favourite_beverage form field
    data = data + "&action=" + data["favorite_beverage"];

    $.ajax({
      type: "POST",
      dataType: "json",
      url: "response.php", //Relative or absolute path to response.php file
      data: data,
      success: function(data) {
        $(".the-return").html(
          "Favorite beverage: " + data["favorite_beverage"] + "<br />Favorite restaurant: " + data["favorite_restaurant"] + "<br />Gender: " + data["gender"] + "<br />JSON: " + data["json"]
        );

        alert("Form submitted successfully.\nReturned json: " + data["json"]);
      }
    });
    return false;
  });
});
</script>