繁体   English   中英

如何从我的HTML表单调用PHP函数?

[英]how to call a php function from my html form?

我有一个网站模板,我想在那里自定义邮件发件人。 我看到这个表格来帮助实现它。

php文件看起来像这样:

<?php
echo 'testing php'; 
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender 
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message 
$receiver = "myEmail@hotmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to 

$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
$send = mail($receiver, 'Contact Form Submission', $body, $email);
if ($send) {
    echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}
?>

UPDATE

我设法像这样调用php文件:

<form id="form" method="post" action="ajaxSubmit.php" > 
                <fieldset> 
                <label><input type="text" id="name" name="name" value="Name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
                  <label><input type="text" id="email" name="email" value="Email" onBlur="if(this.value=='') this.value='Email'" onFocus="if(this.value =='Email' ) this.value=''"></label> 
                <label><input type="text" id="web" name="web" value="Phone" onBlur="if(this.value=='') this.value='Phone'" onFocus="if(this.value =='Phone' ) this.value=''"></label>
                  <label><textarea id="text" name="text" onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label> 
                <input type="reset" /> 
                <input type="submit" /> 
                </fieldset> 
                </form>

但是当我运行这个时,我得到了ERROR

Warning: mail() [function.mail]: SMTP server response: 550 The address is not valid. in C:\wamp\www\forSale\dataTable\ajaxSubmit.php on line 17

但后来我检查了变量的值,它们是正确的。 这意味着什么?

除非我缺少咖啡在我的眼睛上玩耍,否则你没有在这些输入上指定名称属性。 $ _POST不包含元素的ID,而是包含'name'和'value'属性。

例如:

<input type="text" id="name" value="Name" name="name" ...

编辑:要调试此理论,请尝试在PHP文件中输出$ _POST变量的值

将attribute name="field_name"添加到输入字段。 这可能会解决问题。

正如我在聊天中所说,你应该首先尝试通过首先正常提交表单,然后通过使用javascript验证它来改进它,然后尝试用ajax提交它。

如果您将表单修改为基础,则会得到:

<form id="form" method="post" action="ajaxSubmit.php" >
    <fieldset>
        <input type="text" id="name" name="name" value="Name" 
                    onBlur="if(this.value=='') this.value='Name'" 
                    onFocus="if(this.value =='Name' ) this.value=''" />
        <input type="text" id="email" name="email"  value="Email" 
                    onBlur="if(this.value=='') this.value='Email'"
                    onFocus="if(this.value =='Email' ) this.value=''" />
        <input type="text" id="web" name="web"  value="Phone" 
                    onBlur="if(this.value=='') this.value='Phone'" 
                    onFocus="if(this.value =='Phone' ) this.value=''" />
        <textarea id="text" name="text" value="Message"
                    onBlur="if(this.value==''){this.value='Message'}" 
                    onFocus="if(this.value=='Message') this.value=''}" />  
        <input type="reset" />
        <input type="submit" />
    </fieldset>  
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM