gpt4 book ai didi

php - 使用 ajax 验证的单选按钮到 mysql

转载 作者:行者123 更新时间:2023-11-29 08:49:59 26 4
gpt4 key购买 nike

首先:我使用的是 Jeffrey Way 在《In the Woods》中制作的邮件脚本。

我有一个简单的表单,如下所示:

<form method="post" action="sendEmail.php" id="form_reserveren">
<div id="container">

<div id="main" style="width: 100%; height: auto; float: left;">
<div class="left">
<h3>Uw gegevens</h3>
<p style="margin-bottom: 30px; height: 40px; width: 100%;">
<input type="radio" id="zml_geslacht" name="zml0" value="de Heer"/><span style="float: left; margin-top: -5px; height: 25px;">de Heer</span><br />
<input type="radio" id="zml_geslacht" name="zml0" value="Mevrouw"/><span style="float: left; margin-top: -5px; height: 25px;">Mevrouw</span>
</p>
<p>
<input type="text" id="zml_voornaam" name="zml1" value="Uw voornaam" onblur="if(this.value=='') this.value='Uw voornaam';" onfocus="if(this.value=='Uw voornaam') this.value='';"/>
<input type="text" id="zml_achternaam" name="zml2" value="Uw achternaam *" onblur="if(this.value=='') this.value='Uw achternaam *';" onfocus="if(this.value=='Uw achternaam *') this.value='';"/>
</p>
<p>
<input type="text" id="zml_email" name="zml3" value="Uw e-mailadres *" onblur="if(this.value=='') this.value='Uw e-mailadres *';" onfocus="if(this.value=='Uw e-mailadres *') this.value='';"/>
</p>
</div>

<div class="right">
<h3>Uw reservering</h3>
<p style="height:68px;">
<textarea name="zml13" id="zml_opmerkingen" rows="12">Opmerkingen</textarea>
</p>


<p><input type="submit" name="submit" id="submit" value="Email Us!" /></p>
<ul id="response" />
</div>
</div><!--end main -->

</div><!-- end container -->
</form>

这通过 ajax 进行,如下所示:

var zml0 = $('input#zml_geslacht').html( $(':checked').val());
var zml1 = $('input#zml_voornaam').val();
var zml2 = $('input#zml_achternaam').val();
var zml3 = $('input#zml_email').val();
var zml13 = $('textarea#zml_opmerkingen').val();

$.ajax({
type: 'post',
url: 'sendEmail.php',
data: 'zml0=' + zml0 + '&zml1=' + zml1 + '&zml2=' + zml2 + '&zml3=' + zml3 + '&zml13=' + zml13,

success: function(results) {
$('#main img.loaderIcon').fadeOut(1000);
$('ul#response').html(results);
}
}); // end ajax

之后,它会转到我的 sendEmail.php 将表单发送到电子邮件地址(并将其存储在数据库中)。查询是:

 $zml0 = $_REQUEST['zml0']; // Geslacht

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);


// query
$sql = "INSERT INTO test (zml0,zml1,zml2) VALUES (:zml0,:zml1,:zml2)";
$q = $conn->prepare($sql);
$q->execute(array(':zml0'=>$zml0,
':zml1'=>$zml1,
':zml2'=>$zml2));

一切正常..我还有姓名、电子邮件地址等,除了“性别”单选按钮(荷兰语中的“性别”是 Geslacht,我将其命名为 zml0,以进一步防止垃圾邮件发送者)。当然,想法是用户应该选择女性或男性。

当它存储在数据库中时,它只显示[object Object]。当我只是回显输入时也是如此。我怎样才能做到这一点?

最佳答案

试试这个:

var zml0 = $('input[name=zml0]:checked').val();

您可以尝试的另一件事是:

$.ajax({
type: 'post',
url: 'sendEmail.php',
data: $('#form_reserveren').serialize(),

success: function(results) {
$('#main img.loaderIcon').fadeOut(1000);
$('ul#response').html(results);
}
}); // end ajax

关于php - 使用 ajax 验证的单选按钮到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524662/

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