变量未转移到 PHP 页的 URL?
Variable not Carrying Over to URL of PHP Page?
我创建了一个简单的表单,当点击提交时,它将转到我创建的名为 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;有人可以告诉我为什么吗?
错误 1:
在您的表单中,您提到的方法是 POST
,但您尝试构建的 url 似乎是 GET
方法。
错误 2:
onsubmit attribute is for forms not for input elements
<form onsubmit="myFunction()">
</form>
并向我们展示您的 java 脚本以进行更多分析
试试这个:
<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
然后您可以使用 $_POST
.
获得您的 u
变量
eventcreator.php :
$username = $_POST['u'];
echo $username;
我创建了一个简单的表单,当点击提交时,它将转到我创建的名为 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;有人可以告诉我为什么吗?
错误 1:
在您的表单中,您提到的方法是 POST
,但您尝试构建的 url 似乎是 GET
方法。
错误 2:
onsubmit attribute is for forms not for input elements
<form onsubmit="myFunction()">
</form>
并向我们展示您的 java 脚本以进行更多分析
试试这个:
<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
然后您可以使用 $_POST
.
u
变量
eventcreator.php :
$username = $_POST['u'];
echo $username;