Having difficulty with form data - php

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;
}

Related

PLS HELP PHP syntax

Here's my form
<form id="place-bid" action="placebid.php?placeBidProductId='.$row['product_id'].'" method="post" >
Your Bid:<br>
<input type="text" placeholder="$$$" name="money">
<input type="Submit" class="button_bid" name="submit" value="Place Bid">
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
</form>
and here is my placebid.php
<?php
// $product_id=$_GET['placeBidProductId'];
$product_id=$_POST['row_id'];
echo " Id Produs : ".$product_id;
?>
My problem: I can't get the value from $row['product_id'], it will only echo the string "$row['product_id'] .$product_id;"
I'm guessing that before you are outputting that form you are doing some sort of query of a database. If that is the case then you likely want to change this line
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
to
<input type=hidden id='rowid' value="<?php echo row['product_id'];>" name='row_id'>
Use print_r($_POST) for all form post value on form action page i.e. placebid.php. it will show you all value in form. Then make you business logic accordingly.

How to group form's parameters for $_POST?

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>

Simple form submit to PHP function failing

I'm trying to submit two form field values to a PHP function, on the same page.
The function works perfect manually filling the two values.
<?PHP // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>
On the form I must be missing something, or I have a syntax error. The web page doesn't fail to display. I found the below example here: A 1 Year old Stackoverflow post
<?php
if( isset($_GET['submit']) ) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1"></input>
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Could it be the placement of my code on the form?
Any suggestions appreciated.
You need to name your submit button, or check for something else on your if(isset($_GET['submit'])) portion:
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1" />
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2" />
<br></br>
<input type="submit" name="submit" value="send" />
</form>
OR keep same form, but change php to:
<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
You can you hidden field as:
<input type='hidden' name='action' value='add' >
And check on php by using isset function that form has been submitted.

manipulate form input variable before post

BEFORE hitting the submit button on a form, I need to send form variables and their values into a php serialized array (name=>value). And then upon post, I need to pass the array to a php function called modify($vararray). For example:
<form action = "someaction.php" method="post"/>
<input type="text" name="var1" id="var1" <br>
<input type="text" name="var2" id="var2" <br>
<?php
(DO SOMETHING HERE TO CREATE A PHP SERIALIZED ARRAY OF NAME=>VALUE INTO $VARARRAY OF ALL PREVIOUS INPUT VARIABLES ABOVE)
echo "input type=\"hidden\" name=\"modvar" id=\"modvar\" value=\"" . modify($vararray) . "\">\n";
?>
<input type="submit" name="submit" value="Submit"/>
</form>
Any suggestions?
You should use JQuery.serializeArray() for this here you can find an example:
URL: .serializeArray()
I hope this helps!

Simple PHP: getting variable from a form input

I can't really use PHP and from what I've seen in tutorials this should work, but it doesn't:
<html>
<head></head>
<body>
<form>
<input type='text' name="name" value='myName'>
</form>
<p>
<?php
$name = $_POST['name'];
echo $name
?>
</p>
</body>
</html>
Is there a reason I can't get the value of name?
Sorry for asking such a simple question...
here is the fiddle http://jsfiddle.net/DCmu5/1/, so, please try what you said and send it to me only when it works before answering
PHP is a server-side language, so you'll need to submit the form in order to access its variables.
Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:
echo $_GET['name'];
It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:
<form method="post">
<input type='text' name="name" value="myName" />
<input type="submit" name="go" value="Submit" />
</form>
Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.
Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>
<form action="yourpagename.php" method="post">
<input type='text' name="name" value='myName'>
<button type="submit">Submit</button>
</form>
Click button and whatever is typed in your field will show up.
<html>
<body>
<form method="post" action="1.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
try this
<?php
if(isset($_REQUEST['name'])){
$name = $_REQUEST['name'];
echo $name;
}
?>

Categories