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

时间:2016-07-14 18:48:28

标签: php variables twilio html-form

我有一个简单的HTML文件,其中包含一个表单,要求用户提供特定信息,例如姓名,联系方式,位置等。表单有一个动作来发送-sms.php"它使用Twilio API将短信转发到我的手机。

文本发送正确,但我尝试从HTML表单调用变量的方式在我的PHP代码中无法识别。换句话说,我不断在引号内获取文本,而不是提交到输入框中的值。我已经包含以下代码:



<?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",   
)); 
?>
&#13;
<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 (i.e. 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

表单字段中的

id属性 NOT 用于表单提交。只有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

如果您完成了基本调试,例如var_dump($_POST);,您就会看到错误。