简体   繁体   English

在PHP中从HTML表单调用变量?

[英]Calling variables from HTML form in PHP?

I have simple HTML file containing a form which asks the user for specific info such as name, contact, location, etc. The form has an action to "send-sms.php" which uses a Twilio API to forward an SMS to my phone. 我有一个简单的HTML文件,其中包含一个表格,该表格要求用户提供特定信息,例如姓名,联系方式,位置等。该表格具有对“ send-sms.php”的操作,该操作使用Twilio API将SMS转发到我的手机。

The text sends properly, but the way I am attempting to call the variables from the HTML form is not getting recognized in my PHP code. 文本正确发送,但是我尝试从HTML表单调用变量的方式未在我的PHP代码中识别。 In other words, I keep getting the text inside the quotes, but not the values that were submitted into the input boxes. 换句话说,我一直将文本放在引号内,而不是输入到输入框中的值。 I have included the code below: 我已包含以下代码:

 <?php require 'twilio-php-master/Services/Twilio.php'; $name = $_POST['name']; $task = $_POST['task']; $contact = $_POST['contact']; $location = $_POST['location']; $misc = $_POST['misc']; $account_sid = 'AC*******************'; $auth_token = 'b********************'; $client = new Services_Twilio($account_sid, $auth_token); $client->account->messages->create(array( 'To' => "3177302557", 'From' => "+13173644864", 'Body' => "YOU GOT A NEW ERRAND, BET!: \\n Name: $name \\n Task: $task \\n Contact: $contact \\n Location: $location \\n Misc: $misc", )); ?> 
 <form action="send-sms.php" method="POST"> <fieldset> <legend><span class="number">1</span> Your Information</legend> <input type="text" name="field1" id="name" placeholder="Your Name *"> <input type="email" name="field2" id="contact"placeholder="Contact Information (Email, Phone Number, etc.) *"> <input type="location" name="field3" id="location" placeholder="Your Location (ie McNutt, Hodge Hall, exact address, etc.)*"> <input type="text" name="field4" id="misc" placeholder="Miscellaneous Information That May Be Important"></textarea> <label for="job">Urgency:</label> <select id="job" name="field5"> <optgroup label="Urgency level: just for us to prioritize properly"> <option value="Not Urgent">Low (ETA: Up to an hour)</option> <option value="reading">Normal (ETA: Up to 45 mins)</option> <option value="boxing">Critical (ETA: ASAP!)</option> </optgroup> </select> </fieldset> <fieldset> <legend><span class="number">2</span>Task that needs completion</legend> <input type="text" id="task" name="field6" placeholder="Let Us Know How We Can Help!*"></input> </fieldset> <input name="submit" type="submit" value="Submit" onClick="push();validateForm();"/> </form> 

id attributes in form fields are NOT used for form submission. 表单字段中的id属性用于表单提交。 only name is: 唯一的name是:

<input type="text" name="field1" id="name" placeholder="Your Name *">
                          ^^^^^---used for form submission

$foo = $_POST['name']; // warning: undefined index
$foo = $_POST['field1']; // A-OK

If you'd done even BASIC debugging, like var_dump($_POST); 如果您已经完成了BASIC调试,例如var_dump($_POST); , you'd have seen your error. ,您会看到自己的错误。

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

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