Creating an Array with PHP - php

I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?
HTML:
<input type="text" name="interest"/>

I've assumed you're using POST.
You would use
<input type="text" name="interest[]">
Then on the post page, you could use:
foreach($_POST['interest'] as $i){
echo $i. "<br>";
}
or whichever method you wanted to use to get the POST data.
You could also do something like:
<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>
You can then call this data by using:
<?php echo $_POST['interest']['music']; ?>

<input type="text" name="interest[]"/>

You should add square brackets. This triggers PHP to put them in an array like this:
HTML
<input type="text" name="interest[]"/>
<input type="text" name="interest[]"/>
PHP
//Get values
var_dump($_POST['interest']);

Use brackets in your input field to create an array of POST values:
<input type="text" name="interest[]"/>
<?php
var_dump($_POST['interest']); // will be an array
?>

Related

form submit only giving last value in a loop codeigniter

Here is my view
<?php for($i=0;$i<count($acb['def']);$i++) { ?>
<input type="text" name="xyz" value="<?php echo $abc['def'][$i]?>" />?>
Here is my controller
$xxx=$this->input->post('xyz')
Now when i submit the form the last value only gets posted to controller
then i found that the name is same for all fields so it takes last value , so i changed the input name as
name ='xyz[$i]'
Now i need to post values , How to post values with this
You need to send name as array rename it to 'xyz[]' here
<input type="text" name="xyz[]" value="<?php echo $abc['def'][$i]?>" />?>
You use to below code...
<?php for($i=0;$i<count($acb['def']);$i++) { ?>
<input type="text" name="xyz[]" value="<?php echo $abc['def'][$i]?>" />
?>

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

Is POST method used to send user defined inputs only

Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.
The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.
I tired following approach:
user form.php--->
<?php
$count=file_get_contents("cnt.txt","r");
$count=$count+1;
echo"Welcome!<br/>You are student number $count.<br/>";
?>
<html>
<p>Fill in the following information to save your marksheet to the database:</p><br/>
<form action="mysql_insert.php" method="POST">
Name:<input type="text" name="name" value=""/><br/><br/>
Marks(out of 100)<br/>
Subject 1:<input type="text" name="sub1" value=""/><br/>
Subject 2:<input type="text" name="sub2" value=""/><br/>
Subject 3:<input type="text" name="sub3" value=""/><br/>
Subject 4:<input type="text" name="sub4" value=""/><br/>
Subject 5:<input type="text" name="sub5" value=""/><br/><br/>
<p name="count" value="$count"></p>
<input type="submit" value="Submit"/>
</form>
</html>
I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).
And on the receiving end I used following code:
mysql_insert.php--->
<?php
require("connect.php");
$name=$_POST['name'];
$s1=$_POST['sub1'];
$s2=$_POST['sub2'];
$s3=$_POST['sub3'];
$s4=$_POST['sub4'];
$s5=$_POST['sub5'];
$count=$_POST['count'];
.
.
.
?>
Now on the line $count=$_POST['count']; the browser is throwing the error:
Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10
It seems the count is not being posted to this file.
Please guide me where I am wrong.
P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option.
So please help if the non user-input variable can be posted via forms???
Thanks a million!!!
you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..
<input type="hidden" name="count" value="<?php echo $count ?>">
<p name="count" value="$count"></p>
you can not use directly any html tag for post data only use html form fields for posting data.
for use html tag values post you need to use js/ajax/php
<p name="count"><?php echo $count;?></p>
or better you use
<textarea name="count"><?php echo $count;?></textarea>
or for data not showing use hidden field
and get by name this p value or use class or id for get and post data using ajax
<p name="count" value="$count"></p>
This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.
You need to have the value inside input type text or hidden if you want to do the way you are doing now.
Change this line : <p name="count" value="$count"></p>
to the : <input type="hidden" name="count" value="<?=$count?>" />
You can use hidden elements like the following
use
<input type="hidden" value="<?php echo $count; ?>" name="count"/>
instead of
<p name="count" value="$count"></p>
Instead of
<p name="count" value="$count"></p>
Use
<input type="hidden" name="count" value="<?php echo $count;?>">
this line will not work : <p name="count" value="$count"></p>
you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />

reading two form elements with same name

<form action="test.php" method="post">
Name: <input type="text" name="fname" />
<input type="hidden" name="fname" value="test" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
How can I read the values of both the fields named fname?
On my action file(test.php) under $_POST, I am getting only hidden field value.
Is there any PHP setting through which I can read both values?
I believe you want to name the fields as:
Name: <input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
to make PHP understand them as an Array.
In case someone wants to do this and doesn't want to change the name of the form elements, or can't, there is still one way it can be done - you can parse the $_SERVER['QUERY_STRING'] or http_get_request_body() value directly.
It would be something like
$vals=explode('&',http_get_request_body());
$mypost=Array();
foreach ($vals as $val) {
list($key,$v)=explode('=',$val,2);
$v=urldecode($v);
$key=urldecode($key);
if ($mypost[$key]) $mypost[$key][]=$v;
else $mypost[$key]=Array($v);
}
This way $mypost ends up containing everything posted as an array of things that had that name (if there was just one thing with a given name, it will be an array with only one element, accessed with $mypost['element_name'][0]).
For doing the same with query strings, replace http_get_request_body() with $_SERVER['QUERY_STRING']
If you want to pass two form inputs with the same name, you need to make them an array. For example:
<input type="text" name="fname[]" />
<input type="hidden" name="fname[]" value="test" />
You can then access it using the following:
$_POST['fname'][0]
$_POST['fname'][1]
You might want to rethink whether you really need to use the same name though.
Solutions are
1) Try using different name for textbox and hidden value
2) Use an array as mentioned above for field name
3) Its not possible as the values will be overwritten if the names are same

How do I retrieve a PHP variable in my HTML code?

I call a PHP script from my HTML form submit.
Then I process the values retrieved from this HTML form in the PHP script and now I want to display these values in another HTML form that I am calling from this PHP script.
How do I retrieve this variable?
Note: I tried echo but I think I actually want to display this value in the form (HTML) and not break it again in a PHP tag.
I'm not sure what you mean by "not breaking it again with a PHP tag". HTML on its own cannot access PHP variables. This is because PHP is a server-side language and HTML is a client side language. But here is the simplest way to print php variables retrieved from a form.
<form>
<p>Your name: <?php echo $_POST['name'] ?></p>
</form>
This is assuming that there was a form that submitted a field called 'name' using POST.
You can process the name in a php script at the top of the file and then simply echo it when you're printing the html. This way, you won't have too much php code mixed in with the HTML (which makes it look cleaner).
Once you got the values in the PHP script, are you calling a new script? If so, you might wanna save the values in $_SESSION["varible_name"]. If not, you just have to echo it.
It depends on how you are accessing your form data, either through $_POST or through $_GET. For simplicity, I'll assume your using $_GET and modify this example for more clarity.
So lets say you have a form hosted on welcome.php:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
Now the results will be returned back to the same page, so you want to modify the page to say:
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo $_GET["fname"]; ?>"/>
Age: <input type="text" name="age" value="<?php echo $_GET["age"]; ?>" />
<input type="submit" />
</form>
Though you'll notice that we're using the same page, and we can only have one version of the page, so we want to render if upon the condition that our variable has been set.
if (isset($_GET["fname"])){
//code to print second form
}
else{
//code to print first form
}
Or, in another way (using the ternary operator):
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" value="<?php echo ((isset($_GET["fname"]))?$_GET["fname"]:""); ?>"/>
Age: <input type="text" name="age" value="<?php echo ((isset($_GET["age"]))?$_GET["age"]:""); ?>" />
<input type="submit" />
</form>

Categories