提交表单后,如何使用PHP将多个字符串绑定在一起?
我希望在不同页面上提交表单后,多个字符串相互连接 有人能给我解释一下该怎么办吗提交表单后,如何使用PHP将多个字符串绑定在一起?,php,html,string,forms,post,Php,Html,String,Forms,Post,我希望在不同页面上提交表单后,多个字符串相互连接 有人能给我解释一下该怎么办吗 第1条: 第2条: 第3条: 所有的字段看起来都一样: 虽然HTML和HTML允许这样做,但它并不像您想象的那样工作。填写在所有三个输入字段中的字符串将作为相同属性(inputText)的值发送到服务器,并相互覆盖。如果解释器最终将它们存储在脚本可以从中获取的$\u POST['inputText']中,则只有一个 然而,PHP允许一个技巧:如果输入字段的名称以[]结尾,当值到达服务器时,PHP解释器
第1条:
第2条:
第3条:
所有的
字段看起来都一样:
虽然HTML和HTML允许这样做,但它并不像您想象的那样工作。填写在所有三个输入字段中的字符串将作为相同属性(inputText
)的值发送到服务器,并相互覆盖。如果解释器最终将它们存储在脚本可以从中获取的$\u POST['inputText']
中,则只有一个
然而,PHP允许一个技巧:如果输入字段的名称以[]
结尾,当值到达服务器时,PHP解释器不会将它们存储到同一个字符串变量中(后面的值会覆盖前面的值),而是存储在一个数组中
将HTML更改为如下所示:
<form action="script2.php" method="post">
String1: <input type="text" name="inputText[]"/>
<br>
<br>
String2: <input type="text" name="inputText[]"/>
<br>
<br>
String3: <input type="text" name="inputText[]"/>
<br>
<br>
<input type="submit" name="SubmitButton"/>
</form>
第1条:
第2条:
第3条:
然后,在
script2.php
中,您将名为inputText[]
的所有三个文本输入的值放入$\u POST['inputText']
(以这种方式创建)。只需为每个输入指定一个不同的名称即可
<form action="" method="post">
String1: <input type="text" name="input1"/>
<br>
<br>
String2: <input type="text" name="input2"/>
<br>
<br>
String3: <input type="text" name="input3"/>
<br>
<br>
<input type="submit" name="SubmitButton"/>
</form>
第1条:
第2条:
第3条:
在PHP中连接输入
<?php
echo "Totale string: ".$_POST['input1']." ".$_POST['input2']." ".$_POST['input3'];
?>
将此代码用于您的第一页:
<html>
<body>
<form action="script2.php" method="post">
String1: <input type="text" name="str1"/>
<br>
<br>
String2: <input type="text" name="str2"/>
<br>
<br>
String3: <input type="text" name="str3"/>
<br>
<br>
<input type="submit" name="SubmitButton"/>
</form>
</body>
</html>
第1条:
第2条:
第3条:
下面是script2.php的代码:
<?php
$firstname = $_POST['str1'];
$mi = $_POST['str2'];
$lastname = $_POST['str3'];
$fullname = $firstname . " " . $mi . " " . $lastname;
echo "<br> Totale String : ";
echo $fullname;
echo "<br> First Name : ";
echo $firstname;
echo "<br> Middle Initial : ";
echo $mi;
echo "<br> Last Name : ";
echo $lastname;
?>
好的,我将对其进行修改,这样您就可以看到如果只有一页,其中一页将是什么。我还将在这个示例中向您展示我在基本级别上处理过滤
<?php
if(!isset($_POST['submit'])){
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
String1: <input type="text" name="str1" pattern="[A-Za-z']{13}"/>
<br>
<br>
String2: <input type="text" name="str2" pattern="[A-Za-z']{13}"/>
<br>
<br>
String3: <input type="text" name="str3" pattern="[A-Za-z']{13}"/>
<br>
<br>
<input type="submit" name="submit" value="OK"/>
</form>
</body>
</html>
<?php
}else{
/////load the posts into variables
$str1=$_POST['str1'];
$str2=$_POST['str2'];
$str3=$_POST['str3'];
////// then I filter my apostrophe I let through
$str1=str_replace("'","’", $str1);
$str2=str_replace("'","’", $str2);
$str3=str_replace("'","’", $str3);
//////now I will echo it but add a space using html entity
echo $str1." ".$str2."&nsp;".$str3;
}
?>
第1条:
第2条:
第3条:
使用唯一的名称,或使用数组inputText[]
并在PHP中对其进行迭代。请给我们一个示例,说明您希望结果是什么样子。如果您使用inputText[]
则可以执行echo内爆(“”,$_POST['inputText'])代码>。。。或者你可以反复使用$\u POST['inputText']
,foreach($\u POST['inputText']作为$var){…
这听起来像是一个家庭作业问题。。。