【发布时间】:2015-06-23 13:56:26
【问题描述】:
无法将表单数据传递给 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 中的一些问题。 期待帮助 提前致谢:
【问题讨论】: