变量没有携带到PHP页面的URL?

时间:2015-01-03 16:19:12

标签: php html forms variables

我创建了一个简单的表单,当我点击提交时,会转到我创建的名为eventcreator.php的PHP页面;我试图将$ u变量(在这种情况下是用户名)转移到eventcreator.php页面,这意味着如果我在该页面上回显它,它将输出$ u变量。

<div id="pageMiddle">

<form name="eventcreator" action="eventcreator.php?u=<?php echo $u ?>" method="post">

<div>Event Name: </div>
<input type="text" name="eventname" id="eventname" size="50" maxlength="75" />

<div>Month:</div>
<select name="eventdatemonth" id="eventdatemonth" >
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>

<input type="submit" name="submit" class="button3" value="Create My Event!" onsubmit="checkForm();" />

当我使用href时,它似乎适用于我的其他网站页面,例如,如果我使用以下内容:

<p>Click <a href="calendar.php?u=<?php echo $u ?>">here</a> to create an event!</p>

它会成功地将我带到www.mysitename.com/calendar.php$u=sampleusername,但由于某种原因,即使我提交上面的表单,它也只会将我带到eventcreator.php;有谁能告诉我为什么?

2 个答案:

答案 0 :(得分:0)

错误1:

在您的表单中,您提到的方法为POST,但您尝试构建的网址似乎是GET方法。

错误2:

onsubmit attribute is for forms not for input elements

<form onsubmit="myFunction()">
</form>

并向我们展示您的java脚本以分析更多

答案 1 :(得分:0)

试试这个:

<div id="pageMiddle">

<form name="eventcreator" action="eventcreator.php" method="post">

<input type="hidden" name="u" value="<?php echo $u; ?>"/>

<div>Event Name: </div>
<input type="text" name="eventname" id="eventname" size="50" maxlength="75" />

<div>Month:</div>
<select name="eventdatemonth" id="eventdatemonth" >
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>

<input type="submit" name="submit" class="button3" value="Create My Event!" onsubmit="checkForm();" />

在此处,在表单中创建hidden field。因此,在提交表单时,它会将其发送给您eventcreator.php,然后您使用u获取$_POST变量。

eventcreator.php:

$username = $_POST['u'];

echo $username;