How to group form's parameters for $_POST? - php

Ex. (HTML file)
<form name='test' method='POST'>
<input type='text' name='name'>
<input type='text' name='surname'>
<input type='submit' name='sub'>
</form>
Now in the PHP file I'll get the $_POST like:
$_POST['name']= something
$_POST['surname']= something
etc...
What about if I want to "group" that to made a $_POST like this:
$_POST[name-of-the-form]['name']= something
$_POST[name-of-the-form]['surname']= something
etc...
How could I do it?

AbraCadaver is spot on here, all you do is name your form element accordingly.
e.g.
<input type='text' name='name-of-the-form[name]'>

Assuming you are doing this from a PHP file, you should use square braces andn the same name of in all element names:
<?php
$formName = 'test';
?>
<form name='<?=$formName?>' method='POST'>
<input type='text' name='<?=$formName?>[name]'>
<input type='text' name='<?=$formName?>[surname]'>
<input type='submit' name='sub'>
</form>

Related

Form submission return me to index.php

I always have this error when changing any form in my site from post to get and click on submit button redirecting me to index.php.
<form action="index.php?pg=users" method="get">
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit ' name='submit ' value='Search'>
</form>
You might want to update your form a little,
For POST, doing this is fine :
<form action="index.php?pg=users" method="post">
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit' name='submit' value='Search'>
</form>
But for GET, in your form action you have "index.php?pg=users" where "pg=users" is part of your URL query string already.
You can move the "pg=users" as part your form input with type hidden like the example below :
<form action="index.php" method="get">
<input type='hidden' name='pg' value='users'>
<input type='text' placeholder='user name' name='guildn' id='guild'>
<input type='submit' name='submit' value='Search'>
</form>
Remember that in method="GET", your input will be appended to your action URL on form submit and will ignore the query string in action URL.

Saving info while submitting page with GET in PHP

<form method='get' action='y.php'>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['btnSave'])) {
$name=isset(($_GET['txtName'])?isset($_GET['txtName']:'');
//then Logic of insert goes here
}
}
?>
so before moving to y.php the record must be saved.
but I cant get the $name value, as action given to y.php.
How can I get $name which contain value in text box.
if you change the action to this (same/current) page record is going to database without any flaw or error.
try using post method instead and change your code accordingly, try this:
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit' name='submit'/>
</div>
</form>
<?php
if (isset($_POST['submit'])) {
$name=$_POST['txtName'];
//then Logic of insert goes here
//redirect to y.php with name value
echo "<script>window.open('y.php?user=$name','_self')</script>";
}
?>
Then use $nme = $_GET['user']; to get the value of $name in y.php
Try this code,
<form method='get' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if (isset($_GET['txtName'])) {
$name=$_GET['txtName'];
//then Logic of insert goes here
}
}
?>
I suggest you use method post. You can code like this
<form method='post' action=''>
<div>
<input type='text' id='txtName' name='txtName'/>
<input type='submit' value='submit' id='submit'/>
</div>
</form>
<?php
if(isset($_POST['txtName'])){
$name =$_POST['txtName'];
echo $name;
}
?>
you can use the session if you want make the value use in another file.
I hope that can solve your problem

PHP Like Button Problems

I am trying to make a like button for posts on my website.
PHP for like query (d_db_update is
function d_db_update($string) {
return mysql_query($string);
}
)
if($_GET['like']) {
$like = d_db_update("UPDATE posts set post_rating = post_rating+1 WHERE post_id = {$_GET['like']}");
}
Button
<form action='{$_SERVER['PHP_SELF']}&like={$posts_row['post_id']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='submit' name='like' value='Like' /></p>
</form>
What can I do to fix it/make it work?
Use below form with a hidden input it solve your problem.
<form action='{$_SERVER['PHP_SELF']}' method='get'>
<p align='left'>{$posts_row['post_rating']}
<input type='hidden' name='like' value='{$posts_row['post_id']}' />
<input type='submit' value='Like' /></p>
</form>
You are using your form action wrong.. if you are using get method than there is not need to use the form..
try this..
<a href='yourpage.php?like=<?php echo $post_id ?>'>Like</a>
your submit button name and like variable which you have used in action url are the same , and you used get method in method of form.So, you need to change the submit button name.
or
you can do it without using form only on button click try below code
<input type='button' name='like' value='Like' onclick="location.href='yourpage.php?like=<?php echo $post_id ?>'" />
Change your code to this
You can not write PHP variables using {}. You need to echo them out.
<form action='' method='get'>
<p align='left'><?php echo $posts_row['post_rating'] ?>
<input type='hidden' name='like' value='<?php echo $posts_row["post_id"] ?>' />
<input type='submit' value='Like' /></p>
</form>
Edit--
You were not returning the post id correctly, I made the changes, also there is no need to provide any action as it will be self only.

Having difficulty with form data

I have 3 textfields and only one submit button to send all the data of the textfield at once. But how do I send the data of all three textfields at once in php?
<form>
for($x=0;$x<3;$x++){
<input type="text" name="name">
}
<input type="submit" name="submit">
</form>
Now I have a three fields inside a for loop and I have to extract data from all of them using single submit button.So how can I do that?
Using the 'name' attribute on an input allows you to do this for example
<form action='submit.php' method='post'>
<input type='text' name='one'></input>
<input type='text' name='two'></input>
<input type='text' name='three'></input>
<input type='submit' name='submit' value='Submit!' />
</form>
and in your PHP you would do something like this
<?php
if(isset($_POST['submit'])){
$inputOne = $_POST['one'];
$inputTwo = $_POST['two'];
$inputThree = $_POST['three'];
//Do whatever you want with them
}
?>
There are better ways of doing this, but this is probably the simplest to understand
If you want all the inputs to have the same name do this
<input type='text' name='textinput[]'></input>
Use that instead and loop through all of the inputs like so
<?php
foreach($_POST['textinput'] as $input){
//do something with $input
}
?>
You put them all inside the same <form> and make sure they have different values for the name attributes (or that the values end in []).
I believe what you are looking for is this Note the [ ] behind the name field
<form>
for($x=0;$x<3;$x++) {
<input type="text" name="name[]" />
}
<input type="submit" name="submit" />
</form>
Then to retrieve the values
$names = $_POST['name'];
foreach( $names as $name ) {
print $name;
}

Passing hidden values, html

I have a the following php file. displayitems.php
<?php
*
*
*
echo "<form action='http://retailthree.nn4m.co.uk/alex/add_data.html'>";
echo "<input type='hidden' name='value' value='$value'/>";
echo "<button type='submit'>Add</button>";
echo "</form>";
?>
Then the html file. add_data.html:
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>Trend <input type="text" name="trend" size="20"></td>
//many other fields
</tr>
</table>
</forn>
Then the aforementioned html will perform an action on a php file.
However that I want to achieve is to pass the hidden data ->$value from the first php file, to the Trend input box(to print the $value content to the input box). Is this possible?
You would simple use the posted variable and place it in the value attribute of your <input> like so:
<input type="text" value="<?php echo $_GET['value'] ?>" name="trend" size="20">
Of course you should do some validation before echoing it to the <input>
EDIT:
Quite rightly mentioned by #ocanal - GET is the default method for forms. You will not be able to process these forms with PHP if your file is *.html it must be a *.php file.
change the name of add_data.html file to add_data.php use following code in add_data.php file
<?php
// your php code
?>
<form method="post" name="form">
<table id="mytable" border="1">
<tr>
<td>
Trend <input type="text" name="trend" size="20"
value="<?php echo $_POST['trend'] ?>">
</td>
//many other fields
</tr>
</table>
</forn>
I'm a little lost but assuming you mean that you want the hidden value to appear in a text input field on another page I would suggest this:
HTML page
<form name='form' action='yourPhpFile.php' method='POST'>
<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>
</from>
Now for your php file called yourPhpFile.php
<?php
//your value from the hidden field will be held in the array $_POST from the previous document.
//the key depends on your field's name.
$val = $_POST['hiddenGuy'];
echo "<form name='form' action='yourPhpFile.php' method='POST'>
<input name='showingInText' type='text' value='".$val."'/>
</from>";
?>
This can be achieved on the same page too by removing the form action attribute. and echoing the a different input type and value depending on whether $_POST is set using the isset method.
if(isset($_POST['hiddenGuy'])){
echo "<input name='showingInText' type='text' value='".$_POST['hiddenGuy']."'/>";
}
else{
echo "<input name='hiddenGuy' type='hidden' value='hello from the hidden guy'/>
<input type='submit' value='Send'/>";
}

Categories