Before I start, I want to say that I used the search function but none of the other questions helped me.
I found a code that fits what I need (here) but there is a is when I try to retrieve the values.
// Edit
Assuming that i'am using exactly the some code, how to retrieve the values?
The code above is to show what happen when user click in the plus button.
<form method='post' name='form'>
<input type="text" name="username[0]'>
<input type="password" name="password[0]">
// new added fiels using the jquery
<input type="text" name="username[1]'>
<input type="password" name="password[1]">
</form>
Using a form like this, how to retrieve the values one by one?
<?php if (isset($POST['submit'])) { // what to do here? }?>
I ask this question because I want to store the values on a sql database, and I need the separated values like:
User 1 - user & pass
User 2 - user & pass
To use this on a php function that receive the $user and $pass values.
<form method='post' name='form'>
<input type="text" name="username['username'][]'>
<input type="password" name="password['password'][]">
// new added fiels using the jquery
<input type="text" name="['username'][]'>
<input type="password" name="password['password'][]">
<input type = "submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['submit'])){
$postData = $_POST;
for($i = 0; $i < count($postData);$i++){
$username = $postData['username'][$i];
$password = $postData['password'][$i];
}
}
?>
You can echo out each value of the array, or use it any way you want
foreach ($_POST['username'] as $user)
echo $user;
You could use separate arrays for every user - pass you need. For example:
<form method='post' name='form'>
<input type="text" name="user[0]['user']'>
<input type="password" name="user[0]['password']">
// new added fiels using the jquery
<input type="text" name="user[1]['user']>
<input type="password" name="user[1]['password']>
</form>
You read it from php with something like this.
<?php if (isset($_POST['user'])) {
foreach($_POST['user'] as $user){
$username = $user['user'];
$password = $user['password'];
//save into database
}
}?>
And then save into database. You could use php pdo.
http://php.net/manual/en/ref.pdo-mysql.php
You can use different names:
<form method='post' name='form'>
<input type="text" name="username_1">
<input type="password" name="password_1">
// new added fiels using the jquery
<input type="text" name="username_2">
<input type="password" name="password_2">
</form>
<?php
$username_1 = $_POST['username_1'];
....
Or, you can use arrays like this:
<form method='post' name='form'>
<input type="text" name="username[]">
<input type="password" name="password[]">
// new added fiels using the jquery
<input type="text" name="username[]">
<input type="password" name="password[]">
</form>
Your var $_POST['username'] and $_POST['password'] will be arrays. You can iterate the way you want:
foreach($_POST['username'] as $username)
for($i = 0, $c = count($_POST['username']); $i < $c; $i++)
This is pretty straightforward using PHP and HTML. You can look at some examples and documentation here: http://php.net/manual/en/reserved.variables.post.php.
You use an HTML form like this:
<form name="form1" method="post">
<input type="text" name="username1" />
<input type="password" name="password1" />
<input type="text" name="username2" />
<input type="password" name="password2" />
<input type="submit" name="submit" value="Submit" />
</form>
Then you would use PHP to get the values like this:
<?php
if (isset($_POST['submit']))
{
$user1 = $_POST['username1'];
$pass1 = $_POST['password1'];
$user2 = $_POST['username2'];
$pass2 = $_POST['password2'];
}
?>
Of course, use isset() to make sure each username and password is set also, but you get the point.
Related
I would like to store an user input from a textbox into an external php array by clicking a button. Also, there is a dependency from two radio buttons.
This is my current attempt:
At first, I include the external php array:
<?php
include ('../array.php');
?>
The input form:
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
Insert the value into the array:
<?php
if (isset($_POST['send'])){
if (isset ($_POST['name'])){
if ($_POST['name']=='Type1'){
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
}
?>
But the values don't "stack", meaning that the array isn't growing with each button click.
Can anyone help please? :D
Thanks in advance!
If you want to stack values, you need to store them somewhere to keep values between each requests to server.
Values can be stored in session (for current user only) or in any database supported by PHP.
Here an example with session :
index.php :
<?php
include ('./array.php');
?>
<form action="" method="post">
<input type="text" name="textfield" value="">
<input type="radio" name="name" value="Type1">Type1<br>
<input type="radio" name="name" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
array.php :
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['send']) && isset($_POST['name']) && $_POST['name']=='Type1') {
$_SESSION['persistentValues'][] = $_POST['textfield'];
}
print_r($_SESSION['persistentValues']);
?>
<form action="" method="post">
<input type="text" name="textfield" id="txt1" value="">
<input type="radio" name="name" id="id" value="Type1">Type1<br>
<input type="radio" name="name" id="id" value="Type2">Type2<br>
<button id="submit" name="send" type="submit">Save</button>
</from>
in the file php put this
<?php
if (isset($_POST['send']) && $_POST['textfield'] && $_POST['name']){
if ($_POST['name']=='Type1'){
array = require('./array.php');
$newword = $_POST['textfield'];
$array[] = $newword;
}
}
?>
<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" value="">
<br>
Password :
<input type="password" id="password" class="password" name="password" value="">
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" value="">
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" value="">
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_REQUEST['submit']))
{
$res = "insert into register(username, password, phoneno) value('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
if(!empty($_POST)){
echo "Registered!";
}
else {
echo "Try Again!";
}
}
?>
I want to check all $_Post fields check for empty, null or 0 if values of form is not filled show error "try again" and filled show error "registered"? please tell me first is this function is correct or not. if not correct what the correct function.
If you want to validate fields through php, you have to check each field individually. There are some checks you can apply on a data.
if ($_POST['field'] === null) //I would suggest === because it will return false if $_POST['field'] is 0 or empty string
if (empty($_POST['field'])) //to check empty data
if (isset($_POST['field'])) //to check if data exists
If you want to validate your fields like is either string? or digits? or alpha numeric? or all lowercase? or uppercase? There are certain functions which allows you to check field by just calling a single function
Please have a look at this page: CType Functions
You can also use this option. Make all fields required you want to check null or 0, this will make sure that data in field is must before submitting form.
Try changing input field from:
<input type="text" id="username" class="username" name="username" value="">
to:
<input type="text" id="username" class="username" name="username" value="" required >
Also as Rishi said you should also edit query, change values to values
Also 1 tip, you are inserting data in database first and then validating data, according to me it is not good practice, you should first validate data then if all went good then insert it into database, else you will add bad data in database also.
Your final code will be:
<?php require 'db_connect.php'; ?>
<form id="form1" name="form1" method="post" enctype="multipart/form-data" action="register.php" >
Username :
<input type="text" id="username" class="username" name="username" required >
<br>
Password :
<input type="password" id="password" class="password" name="password" required >
<br>
Confirm Password :
<input type="password" id="con_password" class="con_password" name="con_password" required >
<br>
Phone No. :
<input type="text" id="phoneno" class="phoneno" name="phoneno" required >
<br>
<input type="submit" id="submit" name="submit" class="submit" value="Register">
</form>
<?php
if(isset($_POST['submit']))
{
$res = "insert into register(username, password, phoneno) values('".$_POST["username"]."','".$_POST["password"]."','".$_POST["phoneno"]."')";
mysqli_query($conn, $res);
}
?>
I am currently making a report error form that has 4 fields:
Job ID $jobid
Part ID part_id
Machine
Note
The user clicks on a table corresponding the their work and are brought to a new page with a url that has variable. At the moment all the fields are empty however I want the fields to be populated automatically except for notes.
Current Model
Link to report error form:
$EM_html = ''.$tick.'
Report error form:
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Example URL
http://sra-pstest/report_error_form.php?JobID=KANBAN16-09-04-01&Machine=EM&PartID=124047
How do "extract" the information out of the url (JobID, Machine, PartID) and automatically fill out the form?
You can use $_GET
<?php
if(isset($_GET))
{
foreach($_GET as $key=>$value)
{
$$key=$value;
}
echo $JobID."<br>".$Machine."<br>".$PartID;
}
?>
Please try this
<?php
$jobid = #$_REQUEST['JobID'];
$part_id = #$_REQUEST['PartID'];
$machCode = #$_REQUEST['Machine'];
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php print ($jobid) ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php print ($part_id) ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
You use $_GET Method like this code
<?php
$jobid=$part_id=$machine="";
if(isset($_GET['JobID']))
{
$jobid= $_GET['JobID'];
}
if(isset($_GET['Machine']))
{
$machine= $_GET['Machine'];
}
if(isset($_GET['PartID']))
{
$part_id= $_GET['PartID'];
}
?>
<form action="" method="post">
<?php $jobNumber = isset($_GET['JobID']) ? $_GET['JobID'] : '' ?>
Job Number: <input type="text" value="<?php echo jobNumber; ?>" name="jobNum"><br>
<input type="submit" name="submit" value="Submit">
</form>
Try using isset and post method to check if variable are declared and get the variable data on submit of form
<?php
if(isset($_POST['submit'])){
$jobid = $_POST['JobID'];
$part_id = $_POST['PartID'];
$machCode = $_POST['Machine'];
}
?>
<form action="" method="post">
Job Number: <input type="text" value="<?php echo $jobid; ?>" name="jobNum"><br>
Part Number: <input type="text" value="<?php echo $part_id; ?>" name="partNum"><br>
Machine Code: <input type="text" name="machCode" value="<?php echo $machCode; ?>"><br>
Note:<br><textarea rows="5" name="note" cols="30" placeholder="More detail... (Is there a way to recreate the error?)"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
Hope this help
I want to place both register and login form on the same page.
They both starts with:
if (!empty($_POST)) ...
so, I need something like:
if (!empty($_POST_01))... // regForm
and
if (!empty($_POST_02))... //loginForm
Also how to prevent executing first form if the second is busy, and vice versa (user clicks on both)
My idea is to create a simple variable on starting process, for example $x = 1 and at the end of process $x = 0, so:
if ((!empty($_POST_01)) And $x = 0)...
Probably, there is a better way.
You could make two forms with 2 different actions
<form action="login.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
<br />
<form action="register.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="Register">
</form>
Or do this
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="login">
<input type="submit" value="Login">
</form>
<br />
<form action="doStuff.php" method="post">
<input type="text" name="user">
<input type="password" name="password">
<input type="hidden" name="action" value="register">
<input type="submit" value="Register">
</form>
Then you PHP file would work as a switch($_POST['action']) ... furthermore, they can't click on both links at the same time or make a simultaneous request, each submit is a separate request.
Your PHP would then go on with the switch logic or have different php files doing a login procedure then a registration procedure
Well you can have each form go to to a different page. (which is preferable)
Or have a different value for the a certain input and base posts on that:
switch($_POST['submit']) {
case 'login':
//...
break;
case 'register':
//...
break;
}
Give the submit buttons for both forms different names and use PHP to check which button has submitted data.
Form one button - btn1
Form two button -btn2
PHP Code:
if($_POST['btn1']){
//Login
}elseif($_POST['btn2']){
//Register
}
You can use this easiest method.
<form action="validator.php" method="post" id="form1">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="submit" form="form1">
</form>
<br />
<form action="validator.php" method="post" id="form2">
<input type="text" name="user">
<input type="password" name="password">
<input type="submit" value="submit" form="form2">
</form>
Here are two form with two submit button:
<form method="post" action="page.php">
<input type="submit" name="btnPostMe1" value="Confirm"/>
</form>
<form method="post" action="page.php">
<input type="submit" name="btnPostMe2" value="Confirm"/>
</form>
And here is your PHP code:
if (isset($_POST['btnPostMe1'])) { //your code 1 }
if (isset($_POST['btnPostMe2'])) { //your code 2 }
Hope this will help you. Assumed that login form has: username and password inputs.
if(isset($_POST['username']) && trim($_POST['username']) != "" && isset($_POST['password']) && trim($_POST['password']) != ""){
//login
} else {
//register
}
I am trying to POST to a page that has two forms with duplicate name elements. The problem is that one form gets the password value and the other form gets the login value. (I can see this by printing out curl_exec($ch);) I will include my code for the target URL and the formdata. How do I fix this?
// my target url and form data
$target = "http://www.example.com/login";
$formdata = "id=$login&password=$password&Submit=Log In";
Forms:
<form id="login" name="login" method="post" action="login">
<label for="id">LOGIN ID</label> <input type="text" value="" name="id" maxlength="50" size="30"><br>
<label for="password">Password ID</label> <input type="password" name="password" maxlength="12" size="30">
<div align="center"><button class="siteSprite signInSm" value="Log In" name="Submit" type="submit"></button></div>
</form>
<form section="login" id="loginform" name="loginform" action="http://www.example.com/login" method="post">
<input type="text" size="20" value=" Log-in" onfocus="this.value=''" name="id"></td>
<input type="password" value="Password" maxlength="15" size="12" onfocus="this.value=''" name="password">
<input type="submit" class="siteSprite signInSm" value="Sign-In">
</form>
You'll have to do something to indicate which of the two forms got submitted. You can either submit a field with the same name but different values in each one, or use the submit button:
<form ...>
<input type="hidden" name="whichform" value="1" />
<input type="submit" name="Submit" value="form 1" />
</form>
<form ...>
<input type="hidden" name="whichform" value="2" />
<input type="submit" name="Submit" value="form 2" />
</form>
and then
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (($_POST['Submit'] == 'form 1') || ($_POST['whichform'] == '1')) {
.... handle form #1 ....
}
if (($_POST['Submit'] == 'form 2') || ($_POST['whichform'] == '2')) {
.... handle form #1 ....
}
using either method works the same, just pick the one that makes most sense/is easiest and go from there.
$formdata = "id=$login&password=$password&Submit=Sign-In"; might do the trick; note the fact that the second form has a submit button with a value, and the first form has a <button> which won't send a value (or, maybe, sends a different value via script or something)
I just noticed that the submit button doesn't have a name; try passing it with NO submit parameter, i.e.:
$formdata = "id=$login&password=$password