无法将表单数据传递给php 我在Jquery中使用AJAX将表单字段变量传递给php并使用php我希望这些数据应该保存在AEM bin或clientlib中的XML文件中。
我无法使用ajax将这些数据传递给PHP。这就是我正在做的事情:
$('#submit').click(function() {
var n1=$('#Name').val();
var em1=$('#Email').val();
var org1=$('#Org').val();
var ph1=$('#phone').val();
var c1=$('#Country').val();
var ic1=$('#Inquiry_Contact').val();
$.ajax({
type: "POST",
url: "/etc/designs/team-a/clientlibs/scripts/dummy.php",
dataType: "xml",
contentType: "application/xml",
data:"{'Name':'" + n1 + "','email':'" + em1 + "','organisation':'" + org1 + ",'phone':"+ph1+",'country':"+c1+",'inquiry-contact':"+ic1+"}",
success: function (res) {
alert("thank you"+n1+" we will contact you soon");
},
error: function (res) {
alert("XML: not working! " + res.statusText);
}
});
}
这是我的php文件:
<?php
$data=$_REQUEST['data']
$n1 = $_POST['n1'];
$em1 = $_POST['em1'];
$org1 = $_POST['org1'];
$ph1 = $_POST['ph1'];
$c1 = $_POST['c1'];
$ic1 = $_POST['ic1'];
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('data.xml');
$element = $xml->getElementsByTagName('reports')->item(0);
$ic1 = $element->getElementsByTagName('ic1')->item(0);
$n1 = $element->getElementsByTagName('n1')->item(0);
$em1 = $element->getElementsByTagName('em1')->item(0);
$org1 = $element->getElementsByTagName('org1')->item(0);
$ph1 = $element->getElementsByTagName('ph1')->item(0);
$c1 = $element->getElementsByTagName('c1')->item(0);
$newItem = $xml->createElement('reports');
$newItem->appendChild($xml->createElement('ic1', $_POST['ic1']));
$newItem->appendChild($xml->createElement('n1', $_POST['n1']));
$newItem->appendChild($xml->createElement('em1', $_POST['em1']));
$newItem->appendChild($xml->createElement('org1', $_POST['org1']));
$newItem->appendChild($xml->createElement('ph1', $_POST['ph1']));
$newItem->appendChild($xml->createElement('c1', $_POST['c1']));
$xml->getElementsByTagName('reports')->item(0)->appendChild($newItem);
$xml->save('data.xml');
echo "Data has been written.";
?>
但它没有工作....我的事情这个PHP没有被调用...他们只是ajax中的一些问题。 期待着帮助 提前谢谢:
答案 0 :(得分:0)
更改了ajax中的数据,如下所示
$.ajax({ type: "POST", url: "/etc/designs/team-a/clientlibs/scripts/dummy.php", dataType: "xml", contentType: "application/xml", data:{ //changed data like this Name:n1 , email:em1, organisation:org1, phone:ph1, country:c1, inquirycontact:ic1 //changed from inquiry-contact to inquirycontact to avoid syntax errors }, success: function (res) { alert("thank you"+n1+" we will contact you soon"); }, error: function (res) { alert("XML: not working! " + res.statusText); } });
这会将数据作为键值对的数组发送,您的php脚本可以访问。
希望这能解决ajax问题:)