通过其他页面中的PHP脚本插入HTML代码到一个页面 [英] insert html code to a page via php script in other page
问题描述
我对PHP有点新,所以也许这个问题很蠢。
说我有一个表单:
< form id =createNewGalleryname =newgallerymethod =GETaction =code.php>< p>< strong>请选择要上传的图片数量:
< option> 3< / option>
< option> 4< / option>
< option> 5< / option>
< option> 6< / option>
< option> 7< / option>
< option> 8< / option>
< option> 9< / option>
< option> 10< / option>
< / select>< / p>
< input name =submitNumOfPicturestype =submit/>
< / form>
通过提交此表单,我想在此后添加另一个表单,我知道我可以在此特定页面,但我只是想让我的代码不要乱,所以我想在另一个页面上做,说code.php
,这是代码:
<?php
if(isset($ _ GET ['numOfPictures'])){
$ times = $ _GET ['numOfPictures'];
($ i = 1; $ i <= $ times; $ i ++){
echo'< br />选择图片编号'。$ i。':< br /> gt ;';
echo'< input name =file'。$ i。'type =file/>';
echo'< / br>< input name =submitFilestype =submit/>';
}
我得到的结果是新页面中的输出, code.php,但我希望它创建一个表单后,我为我的索引php。
我得到所需的结果,如果我做mt php编码索引页面。
有多种方式可以管理这个,取决于您的最终目标。
您可以使用单独的文件,当用户提交表单时,它会转到下一页,即。
firstform.php - > SUBMIT转到 - > secondform.php,依此类推。
这种方式如果你检查他们的输入,而不是你喜欢什么,你可以把它们发回到前一页/表单,并带有一些错误信息来纠正他们的选择。
您可以像现在一样使用GET或POST
您可以使用一个页面/文件(比如forms.php)并使用PHP来确定哪个阶段用户在。
示例 - 假设您有用户输入他们想要的图片数量,然后询问他们的详细信息(可能不是您所需要的,但您得到流程想法)
<$ ($ _GET ['whichform'] =='')
{
echo'< pre $ p $
// FORM 1 - 获取图片数量
form_id =createNewGalleryname =newgallerymethod =GET
action ='。$ _ SERVER ['PHP_SELF']。'>';
echo'< input type =hiddenname =whichformvalue =form1/>';
echo'< input name =submitNumOfPicturestype =submit/>';
}
// FORM 2 - 提交图片的数量,所以要求提供详细信息
elseif($ _GET ['whichform'] =='form1')
{
echo' < form id =GetUserDetailsname =userdetailsmethod =GET
action ='。$ _ SERVER ['PHP_SELF']。'>';
echo'< input type =hiddenname =whichformvalue =form2/>';
echo'< input name =userdetailstype =submit/>';
}
这会让你从FORM1开始,一旦提交GET信息就会返回true因为如果是FORM1,所以它会显示FORM 2,以此类推FORM 3,4,5等
这在某些方面是一种粗糙的方式,需要调整(即测试提交的数据是否与您一样期望等),但是它可以让你了解如何在一个页面上管理多个表单。
i'm a little bit new for php so maybe this question is stupid.
say i have a form:
<form id="createNewGallery" name="newgallery" method="GET" action="code.php"><p><strong>please choose the number of pictures you want to upload:
</strong>
<select name="numOfPictures" id="numOfPictures">
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select></p>
<input name="submitNumOfPictures" type="submit" />
</form>
by submiting this form i want to add afterwards another form, i know i can do it in this particular page but i just want my code not to be messy, so i want to do it in another page, say code.php
and this is the code:
<?php
if (isset($_GET['numOfPictures'])){
$times = $_GET['numOfPictures'];
for ($i = 1; $i <= $times; $i++){
echo '<br />select picture number '.$i.': <br />';
echo '<input name="file'.$i.'" type="file" />';
}
echo '</br><input name="submitFiles" type="submit" />';
}
the result im getting is an output in a new page, code.php, but i want it to create a form just afterwards the for i had on my index php.
i get the required result if i do mt php coding in the index page.
There are various ways of managing this, depending on your ultimate goal.
You can just use separate files, and when user submits the form, it goes to the next page, ie. firstform.php -> SUBMIT goes to -> secondform.php, and so on. This way if you check their inputs and not what you like, you can send them back to the previous page/form with some error message to correct their choices.
You can Use either GET as you are now, or POST
You can use one page/file (say forms.php) and use PHP to determine what stage the user is at. Example - say you have user enter number of pictures they want, then ask for their details (may not be what you need but you get the flow idea)
//FORM 1 - get number of pictures
if ($_GET['whichform'] == '')
{
echo '<form id="createNewGallery" name="newgallery" method="GET"
action="'.$_SERVER['PHP_SELF'].'"">';
echo '<input type="hidden" name="whichform" value="form1" />';
echo '<input name="submitNumOfPictures" type="submit" />';
}
//FORM 2 - number of pictures submitted so ask for their details
elseif ($_GET['whichform'] == 'form1')
{
echo '<form id="GetUserDetails" name="userdetails" method="GET"
action="'.$_SERVER['PHP_SELF'].'"">';
echo '<input type="hidden" name="whichform" value="form2" />';
echo '<input name="userdetails" type="submit" />';
}
This will let you start with FORM1, once submitted the GET info will return true for if is FORM1, and so it will show FORM 2, and so on for FORM 3, 4, 5, etc It's a crude way in some ways, and needs tweaking (ie testing if the submitted data is as you expect etc) but it gives you the idea on how to manage multiple forms on one page.
这篇关于通过其他页面中的PHP脚本插入HTML代码到一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!