来自具有相同名称的文本框的PHP请求名称
I have multiple textbox with same name, example :
<input type = "text" name = "addCart"/>
<input type = "text" name = "addCart"/>
<input type = "text" name = "addCart"/>
<input type = "text" name = "addCart"/>
If I do a $_Request from php and I want to get the value inputted only from the first and fourth textbox, how can I do it? Thanks
You can't. The last input box's value will overwrite all of the other ones' since they have the same name.
What you could consider doing is using name="addCart[]"
Then, $_REQUEST['addCart'] would be an array.
Like:
<input type="text" name="addCart[]" value="a">
<input type="text" name="addCart[]" value="b">
Then $_REQUEST['addCart'] (or $_POST or $_GET whichever you're using) would contain an array of two strings 'a' and 'b'.
Edit: Just for completeness, I should note that this array is a normal array. Thus $_REQUEST['addCart'][x] where x is some integer index is valid as long as count($_REQUEST['addCart']) > x.
<input type = "text" name = "addCart[]"/>
<input type = "text" name = "addCart[]"/>
<input type = "text" name = "addCart[]"/>
<input type = "text" name = "addCart[]"/>
Access using:
$_REQUEST['addCart'][0]
...
$_REQUEST['addCart'][3]
I don't believe this is possible. try to use slightly different names such as name1, name2 etc
try this, this will post the array of addCart. With $_REQUEST you can acces teh first and fourth texhbox by addCart id
<input type = "text" name = "addCart[]"/>
<input type = "text" name = "addCart[]"/>
<input type = "text" name = "addCart[]"/>
<input type = "text" name = "addCart[]"/>
<?php
echo $_REQUEST['addCart'][0];
echo $_REQUEST['addCart'][3];
?>
If you want to use names of that style when using PHP, then you have to bypass $_POST
or $_GET
, get the raw data and parse it yourself.
For GET requests, then means looking at $_SERVER['REQUEST_URI']
and for POST at file_get_contents('php://input');
If you rename the fields so the names end in []
then they will be presented as an array.
i.e.
<input type = "text" name = "addCart[]"/>
Will be presented as
$_GET['addCart'][] or $_POST['addCart'][]