2017-03-17 84 views
0

您好我想创建一个按钮,将调用PHP函数将产品添加到Shopify从Web应用程序使用Ajax运行PHP函数

首先这是我的result.php文件,该文件是显示亚马逊产品成功 http://codepad.org/MPs4y1Qu

你会发现有两个重要的事情

第一<button type="button">add</button>

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $("button").click(function(){ 
      $.ajax({ 
       type: 'POST', 
       url: 'create_product.php', 
       success: function(data) { 
        prompt(data); 
       } 
      }); 
     }); 
    }); 
</script> 

问题是,当我点击添加按钮时,它显示页面的HTML,但没有发生在create_product.php文件上。我希望它能够调用该函数。另一方面,我在create_product上的代码很好,单独运行100%,但不能与我的web应用程序一起运行。

这里是我的create_product.php代码:

http://codepad.org/at7LtcMK

+0

看起来你实际上并没有将任何数据传递给'create_product.php'。你需要''.ajax'选项中的'data'键,其中包含你想要在你的PHP脚本中使用的信息,这些信息可以通过$ _POST' –

+0

得到,你能否给我一个清楚的答案?我如何通过'data'利用'$ _POST'以及它应该包含哪些内容? –

+0

向Ajax调用添加一个错误处理程序。你也应该取消butotn点击 – epascarello

回答

1

你的AJAX调用将通过邮局发送数据或GET,那么你可以做的是,也什么返回一些东西给你的脚本。这很简单。

http://api.jquery.com/jquery.ajax/

让我们以实例的工作。如果你想使A + B的服务器上,你需要有一个表格,像这样输入:

<form id="yourform"> 
    <div><input name="A" type="text" /></div> 
    <div><input name="B" type="text" /></div> 
</form> 

然后你会编程,一些脚本说你的表单会做什么提交。当你使用jQuery时,让我们使用jQuery:

$("#yourform").submit(function(e) { 
    e.preventDefault(); 
    $.ajax({ 
     type: "POST", //or "GET", if you want that 
     url: "yourfile.php", 
     data: $(this).serializeArray(), //here goes the data you want to send to your server. In this case, you're sending your A and B inputs. 
     dataType: "json", //here goes the return's expected format. You should read more about that on the jQuery documentation 
     success: function(response) { //function called if your request succeeds 
      //do whatever you want with your response json; 
      //as you're learning, try that to see on console: 
      console.log(response); 
     }, 
     error: function(response) { //function called if your request failed 
      //do whatever you want with your error :/ 
     } 
    }); 
}); 

但是你在服务器上做什么? 那么,我只是想返回我的输入,只是为了检查。这里是我的PHP:

<?php 
    header("Content-type: application/json; charset=utf-8"); //that's required to return any JSON data 
    if(isset($_POST, $_POST['A'], $_POST['B'])) 
     exit(json_encode($_POST)); 
    else 
     exit("INVALID REQUEST."); 
?> 

这就是您可以通过AJAX发送信息以在PHP上执行某些操作的方式。

0

您可以添加以下AJAX功能到您的script.Note的AJAX功能发送数据和值triggerPHP到你的PHP代码的页面。因此,在运行php代码的.php页面上,您必须设置一个代码,以便通过$ _POST [] superglobal“捕获”triggerPHP数据并执行任何您想要的操作。 EG

if(isset($_POST['triggerPHP'])){ 
//execute the code here remember to echo json_encode(data) 

}

JQuery的AJAX:

$(document).ready(function(){ 
 
    $("button").click(function(){ 
 

 
    $.ajax({ 
 
    type: 'POST', 
 
    data:'triggerPHP', 
 
    dataType: "json", 
 
    url: 'create_product.php', 
 
    success: function(data) { 
 
    prompt(data); 
 
    }, 
 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
 
    alert("some error"); 
 
    } 
 
    }); 
 
    }); 
 
    });

+0

谢谢@liontass我已经知道了。只有一件事//在这里执行代码时要记得回显'json_encode(data)'在哪个文件上写这个代码? create_product或result.php?它应该放在里面呢? –

+0

回声它必须在create_product.You应该有一个字符串,一个数组的数字HTML代码或脚本 – liontass

+0

我很抱歉,但我很不清楚, 'if(isset($ _ POST ['triggerPHP'])){ //在这里执行代码时记得回应json_encode(data)' 它应该去的地方以及它应该包含的内容?如果你已经检查了我的create_product.php文件http://codepad.org/at7LtcMK,它有这里的所有代码。它有Title,body_html,它必须由我的亚马逊产品替换后点击添加按钮,它应该去我shopify商店 –