gpt4 book ai didi

javascript - 如何将多个表单数据 POST 到 PHP

转载 作者:行者123 更新时间:2023-12-03 11:59:36 24 4
gpt4 key购买 nike

我正在尝试将数据从表单发送到 php,然后将其写入 html,而不加载到 php 文件页面。 var_dump 显示数据已准备好,但不知何故它只是不想传递给 php...

<!DOCTYPE html>
<html lang="en">
<head>
<title>TEST</title>
<meta charset="UTF-8" content="bla bla bla">
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script type="text/javascript" language="javascript">$(function(){$('body').on('click', 'input.sumbit', function(){gogosend();});});</script>
</head>
<body>

<form method="post">
<ul class="form">
<li class="short">
<label>First Name<span class="required"></span></label>
<input type="text" name="first" id="first"/>
</li>

<li class="short">
<label>Last Name<span class="required"></span></label>
<input type="text" name="last" id="last" />
</li>

<li class="long">
<label>Email Address<span class="required"></span></label>
<input type="text" name="email" id="email"/>
</li>

<li class="short">
<label>Company Name</label>
<input type="text" name="company" id="company"/>
</li>

<li class="short">
<label>Telephone Number</label>
<input type="text" name="phone" id="phone" />
</li>

<li class="textarea">
<label>Message<span class="required"></span></label>
<textarea name="message" id="message" rows="20" cols="30"></textarea>
</li>

<li class="button">
<input class="sumbit" name="sumbit" id="sumbit" value="Submit" type="submit" />
</li>
</form>
<script>
function gogosend()

{
var dfirs = document.getElementById("first").value;
var dlast = document.getElementById("last").value;
var demai = document.getElementById("email").value;
var dcomp = document.getElementById("company").value;
var dphon = document.getElementById("phone").value;
var dmess = document.getElementById("message").value;

alert(dfirs);

var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
var data_first = "first=" +dfirs ;
var data_last = "last=" +dlast ;
var data_email = "email=" +demai ;
var data_company = "company=" +dcomp ;
var data_phone = "phone=" +dphon ;
var data_message = "message=" +dmess ;

alert(data_first);

xhr.open("POST", "mailer.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

xhr.send(data_first);
xhr.send(data_last);
xhr.send(data_email);
xhr.send(data_company);
xhr.send(data_phone);
xhr.send(data_message);
}
</script>

<?php
var_dump($_POST);
echo "</br>";
?>

</body>
</html>

这是 php 文件代码:

<?php
$first = $_POST["first"];
$last = $_POST["last"];
$email = $_POST["email"];
$company = $_POST["company"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$text = "NAME: $first $last <br>
EMAIL: $email<br>
COMPANY: $company<br>
TELEPHONE NUMBER: $phone<br>
MESSAGE: $message<br><hr><br><br><br>";
$file = fopen("contactrequests.html","a+");
fwrite($file, $text);
fclose($file);
?>

如何重写上面的内容才能使其工作?例如,现在它为我提供了我输入的随机数据的 var_dump :

array (size=7)
'first' => string '24' (length=2)
'last' => string '225' (length=3)
'email' => string '25g2' (length=4)
'company' => string '2d5' (length=3)
'phone' => string '2d5' (length=3)
'message' => string '2d5' (length=3)
'sumbit' => string 'Submit' (length=6)

我试过How to pass multiple values from ajax to php file但这并没有帮助。

最佳答案

我建议发送一个从表单中提取的 JSON 对象以供 PHP 脚本访问...在 PHP 脚本中使用此函数 http://php.net/manual/en/function.json-decode.php 创建一个 PHP 类实例或从此 JSON 中创建一个索引数组

要将表单序列化为 JavaScript 客户端中的 JSON,您可以使用此 http://api.jquery.com/serializearray/

如果我可以给出建议,请直接跳过使用 xhr...使用 jQuery ajax 包装器...它将确保以相同的方式在所有主要浏览器上运行而无需麻烦

关于javascript - 如何将多个表单数据 POST 到 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com