gpt4 book ai didi

javascript - $_POST 不从 .post 读取变量值

转载 作者:行者123 更新时间:2023-11-30 09:44:21 24 4
gpt4 key购买 nike

这是我的 javascript 代码:

function submitForm() {
var name = document.getElementsByName('name').value
,email = document.getElementsByName('email').value
,subject = document.getElementsByName('subject').value
,body = document.getElementsByName('body').value;

$.post('php/sendForm.php', {name: name,email: email,subject: subject,body: body});

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<div class="form-group inline-block" method="post" action="php/sendForm.php" >
<input type="text" class="form-control" name="name" placeholder="Name"></div>
<div class="form-group inline-block">
<input type="email" class="form-control" name="email" aria-describedby="emailHelp" placeholder="Enter email">
<small id="emailHelp" class="form-text text-muted">Send me an email!</small>
<input type="text" class="form-control" name="subject" aria-describedby="emailHelp" placeholder="Subject">
</div>
<div class="form-group">
<label for="exampleTextarea">Message</label>
<textarea class="form-control" name="body" rows="3">
</textarea>
</div>
<button onclick="submitForm()" type="submit" class="btn btn-primary">Submit </button>
</form>

所有这些都是为了让 php/sendForm.php 读取。但是,我似乎无法读取 php 文件中的名称、电子邮件、主题和正文变量。这是 php 代码:

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];
// The message
$message .= $email. $name. $body ;

最佳答案

您没有对事件调用 preventDefault。这意味着表单已提交(使用 GET 和它自己的 URL,因为这些是默认值)

尝试:

<button onClick="submitForm(); return false;">

关于javascript - $_POST 不从 .post 读取变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558986/

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