Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple $_POST on same page PHP

Tags:

php

I have a question.

Currently this form will be dynamically generated.

Example,

<form method="POST">
<input type="text" name="location" id="location1" />
<input type="submit" value="Submit!" />

<input type="text" name="location" id="location2" />
<input type="submit" value="Submit!" />

<input type="text" name="location" id="location3" />
<input type="submit" value="Submit!" />

<input type="text" name="location" id="location4" />
<input type="submit" value="Submit!" />
</form>

So whenever i press submit, it will take last value of form only. How do i make it take all $_POST?

Thank you.

like image 728
new_webmaster Avatar asked Dec 05 '25 15:12

new_webmaster


2 Answers

You could give each field a unique name: location1, location2....

Alternatively, you could build an array.

To do that, add a [] to each element's name:

<input type="text" name="location[]" id="location1" />

this will give you an array in $_POST["location"].

Don't forget that before using the data (e.g. in a database query or page output) you will need to sanitize each array element separately (using mysql_real_escape_string() or htmlspecialchars() or whatever is needed in your situation.)

like image 59
Pekka Avatar answered Dec 07 '25 04:12

Pekka


Give each input its own name or use:

<input type="text" name="location[]" id="location1" />

Then PHP will treat it like an array.

like image 36
Matthew Avatar answered Dec 07 '25 05:12

Matthew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!