如何从html表单调用php函数?

时间:2022-10-16 13:27:26

I have a template for a website, and I want to customize the message sender there. I saw this form to help out with the implementation of it.

我有一个网站模板,我想定制那里的消息发送者。我看到这个表单来帮助实现它。

the php file looks like this:

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

更新

I've managed to call the php file like this:

我已经像这样调用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>

but when I run this I get teh following 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

but then I check the vallues of the variables, they are correct. what does that mean?

然后我检查变量的价值,它们是正确的。这是什么意思?

3 个解决方案

#1


1  

As I said in the chat, you should try by starting from the ground up by first submitting the form normally, then improve it by validating it with javascript and after that try to submit it with ajax.

正如我在聊天中说的,您应该尝试从头开始,首先正常地提交表单,然后通过使用javascript验证它,然后尝试使用ajax提交表单。

If you modify the form back to basics you get:

如果你把表格修改回基本的,你会得到:

<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>

#2


1  

Unless my lack of coffee is playing tricks on my eyes, you have not specified a name attribute on those inputs. $_POST does not contain the ID of the element, but rather the 'name' and 'value' attributes.

除非我不喝咖啡是在欺骗我的眼睛,否则您没有在这些输入中指定name属性。$_POST不包含元素的ID,而是包含“名称”和“值”属性。

e.g:

例句:

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

edit: to debug this theory, try outputting the values of the $_POST variables in your PHP file

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

#3


1  

add attribute name="field_name" to the input fields. This might fix the issue.

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

#1


1  

As I said in the chat, you should try by starting from the ground up by first submitting the form normally, then improve it by validating it with javascript and after that try to submit it with ajax.

正如我在聊天中说的,您应该尝试从头开始,首先正常地提交表单,然后通过使用javascript验证它,然后尝试使用ajax提交表单。

If you modify the form back to basics you get:

如果你把表格修改回基本的,你会得到:

<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>

#2


1  

Unless my lack of coffee is playing tricks on my eyes, you have not specified a name attribute on those inputs. $_POST does not contain the ID of the element, but rather the 'name' and 'value' attributes.

除非我不喝咖啡是在欺骗我的眼睛,否则您没有在这些输入中指定name属性。$_POST不包含元素的ID,而是包含“名称”和“值”属性。

e.g:

例句:

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

edit: to debug this theory, try outputting the values of the $_POST variables in your PHP file

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

#3


1  

add attribute name="field_name" to the input fields. This might fix the issue.

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