我用jquery和php编写了以下代码,当用户点击保存按钮时提交表单,表单已成功提交,但格式数据为空(发布数据)
这就是我在做的事情:
我正在检查是否单击了保存按钮,如果是,那么我发送邮件并设置表单操作然后提交表单。
<form id= "contactform" method='POST'>
.......
<input type='submit' name='submit_details' value=Save />
</form>
<script type="text/javascript">
function submit_form()
{
$("#contactform").attr("action","https://functions.php");
$("#contactform").submit()
}
<?php
if(isset($_POST['submit_details']))
{
mail("myself@gmail.com", $subject, $mailbody, $headers);
echo "<script> submit_form(); </script>";
}
?>
但是当我直接调用javascript函数时,表单数据将通过
<form id= "contactform" onsubmit='javascript:submit_form();' method='POST'>
那么,这里有什么问题。
答案 0 :(得分:0)
<form id="contactform" method='POST'>
<input type='submit' name='submit_details' value=Save />
</form>
$('input[type="submit"]').click(function(event)
{
event.preventDefault();
$("#contactform").attr("action","https://functions.php");
$("#contactform").submit();
});
答案 1 :(得分:0)
也许我得到了你需要的东西,基本上你想要的东西:
因此,如果工作流程是这个,您可以尝试:
HTML:
<form id="contactform" method="post" action="">
<!-- your other inputs go here? -->
<input type="text" name="input1" value="" />
<!-- I'm just guessing this ones.. -->
<input type="submit" name="submit_details" value="Salve" />
</form>
JS:
$('#contactform').submit(function({
var dataString = $(this).serialize();
$.ajax({
url: 'functions.php',
type: 'POST',
data: dataString,
success:function(response){
alert(response);
}
});
return false;
});
PHP functions.php
if($_POST['submit_details'])
{
//now send email:
if(mail("myself@gmail.com", $subject, $mailbody, $headers))
{
//mail sent: do the rest
$input1 = isset($_POST['input1']) ? $_POST['input1'] : '';
// and so on for the rest of the inputs.
// do whatever you need to.
return 'Mail sent and data saved!';
}
else
{
return 'Couldn\'t send mail!';
}
}
答案 2 :(得分:0)
只需将按钮类型更改为“按钮”
即可 <input type='button' name='submit_details' value=Save onclick="submit_form()" />