How to Get Multiple Value from Post Variable Using Same Name

How can i get multiple value from POST variable using same name

I hope I understand what you want. You want to access the ctext for each individual $pen when printing the corresponding form.

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 with id 1 this will result in 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'].

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.

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.

PHP Get multiple values from GET with same name

I dont think without using array you cant retrieve data of same name. Example

<input type="text" name="color[]" value="blue">
<input type="text" name="color[]" value="green">
<input type="text" name="color[]" value="black">

now datas will pass like this

?color[]=Blue&color[]=Green&color[]=Black  
$color=$_GET['color'];
print_r($color);

Array
(
[color] => Array
(
[0] => Blue
[1] => Green
[2] => Black
)
)

HTML select one name for multiple $_POST variables

To avoid an explicit script block (also avoiding jquery dependency), use the following shorthand:

<!--this should be hidden, set to text Just for preview--><input type="text" name="a2" value="1">
<select name="a1" onchange="document.getElementsByName('a2')[0].value=this.value"><option value='1'>one</option><option value='2'>two</option><option value='3'>three</option></select>

Correct way to pass multiple values for same parameter name in GET request

Indeed, there is no defined standard. To support that information, have a look at wikipedia, in the Query String chapter. There is the following comment:

While there is no definitive standard, most web frameworks allow
multiple values to be associated with a single field.[3][4]

Furthermore, when you take a look at the RFC 3986, in section 3.4 Query, there is no definition for parameters with multiple values.

Most applications use the first option you have shown: http://server/action?id=a&id=b. To support that information, take a look at this Stackoverflow link, and this MSDN link regarding ASP.NET applications, which use the same standard for parameters with multiple values.

However, since you are developing the APIs, I suggest you to do what is the easiest for you, since the caller of the API will not have much trouble creating the query string.

PHP - $_POST variables mixing with variables having the same name

The problem probably lies with register_globals in php's .ini file. Turn this off, restart php and it should be fixed.

Try this to check the setting at the moment of execution of the code:

echo ini_get("register_globals");

Loop $_POST variables with multiple similar names

I would suggest something like that:

HTML markup:

<form>
<input name="aspect[id][]"/>
<input name="aspect[time][]"/>
<input name="aspect[day][]"/>
<input name="aspect[room][]"/>
</form>

Processing PHP code:

<?php
foreach($_POST['aspect'] as $key => $values) {
foreach($values as $index => $value) {
// do something with $key, $index and $value
}
}

And a general remark: do yourself a big favor and keep the code that creates/defines the form in a separate file from the code that processes the form. I do know that most beginners tutorials show both things mixed together, but that is a very stupid thing to do, actually.

How to read multiple values in a single POST variable

You can simply explode() your string using | as the delimiter:

<?php
$urls = explode("|", $_POST['IMG']);

echo $urls[0]; // url.com/img1.png
echo $url[1]; // url.com/img2.png
echo $url[2]; // url.com/img3.png


Related Topics



Leave a reply



Submit