When user inputs text in 'ctext' field and press accept, I want to fill the value=" " field with user input, i achieved this but it fills all the value fields of same name in the page, how can i achieve it for different value of different ctext input? Anyone please give me solution with example, Many thanks
当用户在'ctext'字段中输入文本并按下accept时,我想用用户输入填充value =“”字段,我实现了这个但是它填充了页面中同名的所有值字段,我怎样才能实现它不同ctext输入的不同值?有人请给我解决方案,非常感谢
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
$i=0;
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here" value="<?php $ctext = false; if(isset($_POST['ctext'])){ $ctext = $_POST['ctext']; } echo $ctext[$i]; ?>"></input>
<input type="hidden" name="id" value="<?php $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
$i++;
endwhile;
endif;
endif;
?>
?>
3 个解决方案
#1
0
I hope I understand what you want. You want to access the ctext
for each individual $pen
when printing the corresponding form.
我希望我明白你想要的。您希望在打印相应表单时访问每个$ pen的ctext。
You just need to name your <input>
with a unique name and then access that value when printing. A possible solution is this:
您只需要使用唯一名称命名,然后在打印时访问该值。一个可能的解决方案是:
<input type="text" name="ctext[<?php echo $pen['id']; ?>]" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } echo $ctext; ?>"></input>
What does it do?
它有什么作用?
-
name="ctext[<?php echo $pen['id']; ?>]"
ensures a unique name for each$pen
. For a$pen
withid
1 this will result inname="ctext[1]"
. - name =“ctext [<?php echo $ pen ['id'];?>]”确保每个$ pen的唯一名称。对于id为1的$ pen,这将导致name =“ctext [1]”。
-
if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; }
uses$pen['id']
to look up the corresponding value in$_POST['ctext']
. - if(isset($ _ POST ['ctext'] [$ pen ['id']])){$ ctext = $ _POST ['ctext'] [$ pen ['id']];使用$ pen ['id']查找$ _POST ['ctext']中的相应值。
By the way, when outputting user input you should always escape it, e.g. with htmlspecialchars
. This will look like this: echo htmlspecialchars($ctext);
That way malicious input like "><script>alert('Hello!')</script>
won't execute the javascript.
顺便说一句,当输出用户输入时,你应该总是逃避它,例如与htmlspecialchars。这将是这样的:echo htmlspecialchars($ ctext);这样,像“>
Update: as requested a solution using session to store data:
更新:根据请求使用会话存储数据的解决方案:
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
session_start();
if (isset($_POST['ctext'])) {
$_SESSION['ctext'][$_POST['id']] = $_POST['ctext'];
}
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_SESSION['ctext'][$pen['id']])){ $ctext = $_SESSION['ctext'][$pen['id']]; } echo htmlspecialchars($ctext); ?>"></input>
<input type="hidden" name="id" value="<?php echo $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
endwhile;
endif;
endif;
Note: I removed the now unnecessary counter $i
. The session handling is mainly done before the while
loop (start a session and store POST data). During output the values from the session are used. The name of the input is not an array anymore.
注意:我删除了现在不必要的计数器$ i。会话处理主要在while循环之前完成(启动会话并存储POST数据)。在输出期间,使用会话中的值。输入的名称不再是数组。
#2
0
Change name of an input to an array.like this . When you submit the form you will get these values as an array. Give it a try
将输入的名称更改为数组。就像这样。提交表单时,您将获得这些值作为数组。试一试
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here"></input>
#3
0
I guess your code is misleading you, your form is in while loop so once any of the ctext input is filled your variable $_POST['ctext'] is set on server side and according to your code it sets all the value of ctext once accept is pressed.
我猜您的代码会误导您,您的表单处于while循环中,因此一旦填充了任何ctext输入,您的变量$ _POST ['ctext']就会在服务器端设置,并根据您的代码设置ctext的所有值接受按下。
You can have different names as a solution or an array indexing in input field name=“ctext[]” to avoid this.
您可以在输入字段名称=“ctext []”中将不同的名称作为解决方案或数组索引来避免这种情况。
#1
0
I hope I understand what you want. You want to access the ctext
for each individual $pen
when printing the corresponding form.
我希望我明白你想要的。您希望在打印相应表单时访问每个$ pen的ctext。
You just need to name your <input>
with a unique name and then access that value when printing. A possible solution is this:
您只需要使用唯一名称命名,然后在打印时访问该值。一个可能的解决方案是:
<input type="text" name="ctext[<?php echo $pen['id']; ?>]" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } echo $ctext; ?>"></input>
What does it do?
它有什么作用?
-
name="ctext[<?php echo $pen['id']; ?>]"
ensures a unique name for each$pen
. For a$pen
withid
1 this will result inname="ctext[1]"
. - name =“ctext [<?php echo $ pen ['id'];?>]”确保每个$ pen的唯一名称。对于id为1的$ pen,这将导致name =“ctext [1]”。
-
if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; }
uses$pen['id']
to look up the corresponding value in$_POST['ctext']
. - if(isset($ _ POST ['ctext'] [$ pen ['id']])){$ ctext = $ _POST ['ctext'] [$ pen ['id']];使用$ pen ['id']查找$ _POST ['ctext']中的相应值。
By the way, when outputting user input you should always escape it, e.g. with htmlspecialchars
. This will look like this: echo htmlspecialchars($ctext);
That way malicious input like "><script>alert('Hello!')</script>
won't execute the javascript.
顺便说一句,当输出用户输入时,你应该总是逃避它,例如与htmlspecialchars。这将是这样的:echo htmlspecialchars($ ctext);这样,像“>
Update: as requested a solution using session to store data:
更新:根据请求使用会话存储数据的解决方案:
<?php
$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);
if($result):
if(mysqli_num_rows($result)>0):
session_start();
if (isset($_POST['ctext'])) {
$_SESSION['ctext'][$_POST['id']] = $_POST['ctext'];
}
while( $pen = mysqli_fetch_assoc($result) ):
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">
<div class="name pen-<?php echo $pen['id']; ?>">
<input type="text" name="ctext" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_SESSION['ctext'][$pen['id']])){ $ctext = $_SESSION['ctext'][$pen['id']]; } echo htmlspecialchars($ctext); ?>"></input>
<input type="hidden" name="id" value="<?php echo $pen['id']?>"></input>
</div>
<div class="btn-custom">
<input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
</div>
</form>
<?php
endwhile;
endif;
endif;
Note: I removed the now unnecessary counter $i
. The session handling is mainly done before the while
loop (start a session and store POST data). During output the values from the session are used. The name of the input is not an array anymore.
注意:我删除了现在不必要的计数器$ i。会话处理主要在while循环之前完成(启动会话并存储POST数据)。在输出期间,使用会话中的值。输入的名称不再是数组。
#2
0
Change name of an input to an array.like this . When you submit the form you will get these values as an array. Give it a try
将输入的名称更改为数组。就像这样。提交表单时,您将获得这些值作为数组。试一试
<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here"></input>
#3
0
I guess your code is misleading you, your form is in while loop so once any of the ctext input is filled your variable $_POST['ctext'] is set on server side and according to your code it sets all the value of ctext once accept is pressed.
我猜您的代码会误导您,您的表单处于while循环中,因此一旦填充了任何ctext输入,您的变量$ _POST ['ctext']就会在服务器端设置,并根据您的代码设置ctext的所有值接受按下。
You can have different names as a solution or an array indexing in input field name=“ctext[]” to avoid this.
您可以在输入字段名称=“ctext []”中将不同的名称作为解决方案或数组索引来避免这种情况。