I have two pages in php the first contain a form which is:
<form method="post" action="addnames.php">
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="text" name="name" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
this takes the data to other php page where I am using foreach to read the request in this way:
foreach($_REQUEST['name'] as $name){
//MY CODE
}
So what is the problem?
If you want to get name as an array then you need to change your form code:
<form method="post" action="addnames.php">
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
Now you can get all names in your post request.
Because only one name is sent to the server, and it's a string then not an array. To send an array of names, change your input name to name="name[]" to identify it as an array
<input type="text" name="name[]" placeholder="Name" />
...
Try this code:
<?php
foreach($_REQUEST['name'] as $name){
//MY CODE
}?>
<form method="post" action="addnames.php">
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="text" name="name[]" placeholder="Name" /><br />
<input type="submit" value="Done" />
</form>
Related
I added a "contact us" form to a working website.
When I tried it, I noticed that the submit button was not working. I removed the jquery CDN link from the code and after that, the submit button started to work and emails are now sending smoothly.
Can anyone suggest a solution to keep the jquery link and the submit button working properly ?
<form action="" method="POST" id="myForm">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" /> <br />
<input type="text" name="subject" placeholder="Subject" /> <br />
<input type="text" name="phone" placeholder="Phone" /> <br />
<input type="text" name="emailid" placeholder="Email" /> <br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> <br />
<input type="button" name=" onclick=" myFunction() " value="Submit form"> </fieldset>
</form>
(...)
<script>
function myFunction() { document.getElementById("myForm").submit(); }
</script>
My guess is that's a typo error, look at your code:
<form action="" method="POST" id="myForm"> <fieldset> <input type="text" name="fullname" placeholder="Full Name" /> <br /> <input type="text" name="subject" placeholder="Subject" /> <br /> <input type="text" name="phone" placeholder="Phone" /> <br /> <input type="text" name="emailid" placeholder="Email" /> <br /> <textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> <br /> <input type="button" name=" onclick="myFunction()" value="Submit form"> </fieldset> </form>
There is a missing quotation mark ", just after name=":
<input type="button" name=" onclick=" myFunction() " value="Submit form">
So here is your corrected code.
<form action="" method="POST" id="myForm">
<fieldset>
<input type="text" name="fullname" placeholder="Full Name" /> <br />
<input type="text" name="subject" placeholder="Subject" /> <br />
<input type="text" name="phone" placeholder="Phone" /> <br />
<input type="text" name="emailid" placeholder="Email" /> <br />
<textarea rows="4" cols="20" name="comments" placeholder="Comments"></textarea> <br />
<input type="button" name="" onclick=" myFunction() " value="Submit form"> </fieldset>
</form>
Always double-check your code, and work on clean-formatted code, it will help you to catch such mistakes.
I have been having problems with the second button not running like the first button. this is the code I have:
<p>
<form method="POST">
<input placeholder="Username" type="text" name="username"><br /><br />
<input placeholder="password" type="password" name="password"><br /><br />
<input value="Login" type="submit" name="log_In">
</form>
</p>
</div>
<?php
if(isset($_POST['log_In'])) {
#$f_name = $_POST['fname'];
#$s_name = $_POST['sname'];
#$stud_Id = $_POST['studId'];
#$uname = $_POST['uname'];
#$pass = $_POST['pass'];
#$rpass = $_POST['rpass'];
#$email = $_POST['email'];
#$remail = $_POST['remail'];
#var_dump($f_name);
header("Location:home.php");
}
?>
</div>
<div align="right">
<div>
<p>
<h2>Sign Up</h2>
</p>
<p>
<form>
<input placeholder="Forename" type="text" name="fname" id="Forename"><br /><br />
<input placeholder="Surname" type="text" name="sname"><br /><br />
<input placeholder="Student Id" type="text" name="studId"><br /><br />
<input placeholder="Username" type="text" name="uname"><br /><br />
<input placeholder="password" type="password" name="pass" min="6" max="32"><br /><br />
<input placeholder="Re-type password" type="password" name="rpass" min="6" max="32"><br /><br />
<input placeholder="Email" type="" name="email"><br /><br />
<input placeholder="Re-type Email" type="remail" name="remail"><br /><br />
<input value="Sign Up" type="submit" name="sign_Up">
</form>
</p>
</div>
<?php
if(isset($_POST['sign_Up'])) {
header("Location:home.php");
}
?>
</div>
"if(isset($_POST['sign_up'])) {" is not being run and is just refreshing the page and removing all items from the form.
thanks
By default <form> method is GET. So if(isset($_POST['sign_Up'])) won't work. Change it to if(isset($_GET['sign_Up'])).
Or change your second form tag to:
<form method="POST">
Remember not to use header function after generating HTML content, move it to top!
header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
So it will be better if it is like this:
<?php
if(isset($_POST['log_In']) || isset($_POST['sign_Up'])) {
header("Location:home.php");
}
?>
<form method="POST">
<input placeholder="Username" type="text" name="username">
<br />
<br />
<input placeholder="password" type="password" name="password">
<br />
<br />
<input value="Login" type="submit" name="log_In">
</form>
<div align="right">
<div>
<p>
<h2>Sign Up</h2>
</p>
<p>
<form method="post">
<input placeholder="Forename" type="text" name="fname" id="Forename">
<br />
<br />
<input placeholder="Surname" type="text" name="sname">
<br />
<br />
<input placeholder="Student Id" type="text" name="studId">
<br />
<br />
<input placeholder="Username" type="text" name="uname">
<br />
<br />
<input placeholder="password" type="password" name="pass" min="6" max="32">
<br />
<br />
<input placeholder="Re-type password" type="password" name="rpass" min="6" max="32">
<br />
<br />
<input placeholder="Email" type="" name="email">
<br />
<br />
<input placeholder="Re-type Email" type="remail" name="remail">
<br />
<br />
<input value="Sign Up" type="submit" name="sign_Up">
</form>
</p>
</div>
</div>
You forgot to add method="post"
<form method="post">
<input placeholder="Forename" type="text" name="fname" id="Forename"><br /><br />
<input placeholder="Surname" type="text" name="sname"><br /><br />
<input placeholder="Student Id" type="text" name="studId"><br /><br />
<input placeholder="Username" type="text" name="uname"><br /><br />
<input placeholder="password" type="password" name="pass" min="6" max="32"><br /><br />
<input placeholder="Re-type password" type="password" name="rpass" min="6" max="32"><br /><br />
<input placeholder="Email" type="" name="email"><br /><br />
<input placeholder="Re-type Email" type="remail" name="remail"><br /><br />
<input value="Sign Up" type="submit" name="sign_Up">
</form>
in second form you have not define the method if method is not defined it will accept the GET method default so change your second form tag by
<form action="" method="POST">
i have a form, the carries two textfields with the name attributes "name" and "amount"
<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="name" value="wole" size="32" required="required" />
<input type="text" name="amount" value="100" size="32" required="required" />
<input type="text" name="name" value="yetunde" size="32" required="required" />
<input type="text" name="amount" value="200" size="32" required="required" />
</form>
my intention is to capture name and amount side by side in two rows, based on how my form is structured.
i have tried submittin the data into the database using this snippet
$sql="INSERT INTO records(name, amount)VALUES('$_POST[name]', '$_POST[amount]')";
but it only submits one row of data, "wole and 100", how can i make sure i capture the two different rows from my form
As requested, here is the full code.
I added a new field "score" to show you how to add new fields.
<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="studentname[]" value="wole" size="32" required="required" />
<input type="text" name="course[]" value="100" size="32" required="required" />
<input type="text" name="score[]" value="100" size="32" required="required" />
<input type="text" name="studentname[]" value="yetunde" size="32" required="required" />
<input type="text" name="course[]" value="200" size="32" required="required" />
<input type="text" name="score[]" value="100" size="32" required="required" />
</form>
<?php
$con=mysqli_connect
("localhost","root","famakin","results");
//Checkconnection
if(mysqli_connect_errno())
{
echo"FailedtoconnecttoMySQL:".mysqli_connect_error();
}
foreach($_POST['studentname'] as $idx => $studentname) {
$sql="INSERT INTO records(studentname, course, score)VALUES('" . $studentname . "', '" . $_POST['course'][$idx] . "', '" . $_POST['score'][$idx] . "')";
if(!mysqli_query($con,$sql))
{
die('Error:'.mysqli_error($con));
}
}
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=result.php">';
mysqli_close($con);
?>
You might want to use square brackets with form element names:
<input type="text" name="amount[]" value="100" size="32" required="required" />
in such a case you will receive an array of values in your $_POST['amount'] and then you are responsible for parsing it properly and inserting as many rows as you want, e.g. with foreach php loop or like.
you can use array notation in names
<form method="post" name="form1" id="form1" action="test.php">
<input type="text" name="name[]" value="wole" size="32" required="required" />
<input type="text" name="amount[]" value="100" size="32" required="required" />
<input type="text" name="name[]" value="yetunde" size="32" required="required" />
<input type="text" name="amount[]" value="200" size="32" required="required" />
</form>
in php you can get theses as
$_POST[amount][index_no_here];
Eventually you can put it in loop to alter for each index
As others have said, it might be best to just use an array of fields. If it absolutely has to be one field, you could just split the string into an array by some separator (comma, semi-colon, etc) in your business layer and then loop through it to create different records in the database.
I have a form:
<form action="<?php echo the_permalink(); ?>" method="POST">
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />
<?php echo $firstnameError; ?>
<input type="text" name="email" value="" placeholder="Yoyr Email" />
<?php echo $emailError; ?>
<br>
<input type="text" name="company" value="" placeholder="Your Company Name" />
<input type="text" name="phone" value="" placeholder="Your Phone number" />
<textarea name="project" rows="4" cols="50" placeholder="Describe the scope of work and the most important features of the project *'"></textarea>
<?php echo $addressError; ?>
<br>
<input type="radio" name="budget" value="1500" />
<input type="radio" name="budget" value="2500" />
<input type="radio" name="budget" value="5000" />
<input type="radio" name="budget" value="10000" />
<input type="radio" name="budget" value="100001" />
<input type="radio" name="budget" value="not sure" />
<input type="hidden" name="submit" value="1" />
<input type="submit" value="SUbmit" />
</form>
It actions to the same page, but when I do print_r($_POST); it does not print anything, i.e. no value in $_POST.
What could be the reason(s) for this? I studied a few questions on SO on this but none gave me the answer I was looking for.
If your passing the name as a Post value, wordpress DOSNT like this!
change this
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />
to
<input type="text" name="thename" value="" placeholder="Your First and Last Name *" />
changing the name to thename, Will work guaranteed! ;)
<form action="<?php the_permalink(); ?>" method="POST">
You don't need to echo the_permalink().
This works for me:
<?php print_r($_POST);?>
<form action="" method="POST">
<input type="text" name="name" value="" placeholder="Your First and Last Name *" /><?php echo $firstnameError; ?><input type="text" name="email" value="" placeholder="Yoyr Email"/><?php echo $emailError; ?><br>
<input type="text" name="company" value="" placeholder="Your Company Name"/><input type="text" name="phone" value="" placeholder="Your Phone number"/>
<textarea name="project" rows="4" cols="50"placeholder="Describe the scope of work and the most important features of the project *'"></textarea><?php echo $addressError; ?><br>
<input type="radio" name="budget" value="1500" /><input type="radio" name="budget" value="2500" /><input type="radio" name="budget" value="5000" /><input type="radio" name="budget" value="10000" /><input type="radio" name="budget" value="100001" /><input type="radio" name="budget" value="not sure" />
<input type="hidden" name="submit"value="1"/>
<input type="submit" value="SUbmit" />
</form>
change this
acton="<?php echo the_permalink(); ?>"
to
action="<?php echo the_permalink(); ?>"
I found same problem in my project and solution is in $_POST, maybe you use lower case in your code, change it to upper case.
change $_post to $_POST !
SOLUTION:
due to request problems, in wordpress sites instead of
<form action="http://example.com/">...
You may need to point the .php file.. example:
<form action="http://example.com/index.php">...
//////p.s. echo is automatically done with the_permalink() [same is: echo get_permalink()
I have this Landing Page that I need to pre-populate with form values after it is submitted. Essentially in the end I have to make the url look like this...
http://example.com/r.php?sid=xx&pub=xxxxx&c1=&c2=&c3=&append=1&firstname=Test&lastname=Smith&address=2211+Commerce+St.&city=Dallas&state=TX&zipcode=75080&email=test#test.com
What I currently have now for the form is...
<form name="regForm" method="get" action="http://example.com/r.php?sid=xx&pub=xxxx&c1=&c2=&c3=&append=1">
<input id="firstname" class="text" type="text" name="firstname"/><br>
<input id="lastname" class="text" type="text" name="lastname" /><br>
<input id="email" class="text" type="text" name="email" /><br>
<input id="address" class="text" type="text" /><br>
<input id="city" class="text" type="text"/><br>
<input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
<input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>
How do i create that URL above after the form is submitted? I have been racking my brain all day on this and can't figure it out, i feel like im close.
thanks for the help!
Include the extra parameters as hidden form fields instead of inline query parameters:
<form name="regForm" method="get" action="http://example.com/r.php">
<input type="hidden" name="sid" value="xx" />
<input type="hidden" name="pub" value="xxxx" />
<input type="hidden" name="c1" value="" />
<input type="hidden" name="c2" value="" />
<input type="hidden" name="c3" value="" />
<input type="hidden" name="append" value="1" />
<input id="firstname" class="text" type="text" name="firstname"/><br>
<input id="lastname" class="text" type="text" name="lastname" /><br>
<input id="email" class="text" type="text" name="email" /><br>
<input id="address" class="text" type="text" /><br>
<input id="city" class="text" type="text"/><br>
<input id="zipcode" class="text" type="text" maxlength="5" name="zipcode" /><br>
<input type="submit" value="Send Me My FREE List" id="submitBtn2"/>
</form>
The problem is the input field get variables are causing your url get variables to be truncated, put all your url parameters as hidden values.
<input id="pub" type="hidden" name="pub" value=""/>
<input id="sid" type="hidden" name="sid" value=""/>
I'm assuming that you like the URL that it generates except that you're missing the sid, pub, c1, c2, c3, and append variables. If so, just make hidden inputs like this:
<form id="regForm" ...>
<input id="sid" name="sid" value="" type="hidden" />
<input id="pub" name="pub" value="" type="hidden" />
...
</form>
If you know the values while you're creating the form, then you can do it on the server side. If you don't, then assuming you're using jQuery, you will have to do something like this:
$(function() {
$('#regForm').submit(function () {
$('#sid').val('myNewSidValue');
$('#pub').val('myNewPubValue');
...
});
});