php form submit in div fails - php

after many days of trying to get a previously working php form converted to submitting the variables inside a new div I realized that I'm missing something. Other posts show javascript, but Iv'e never used that before and don't understand the need. The new page draws correctly, but the php variables are not being received on the destination page.
HTML for the submit,
<form action="entrance2.php">
<div class="medium-12 columns m-b20"><h4 class="heading">Existing users log-in here :-</h4></div>
</div>
<div class="row">
<div class="user medium-12 columns text-center m-b15"><img src="images/user-img.png" alt=""/></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="User Name"></label>
<input id="OwnerEmaili" type="text" placeholder="User Name" name="UserName"></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="Password"></label>
<input id="OwnerPasswordi" type="password" placeholder="Password" name="Password"></div>
</div>
<div class="row">
<div class="medium-12 columns text-center"><button class="grd-button">Log In</button></div>
<input type="submit" id="save" name="save" value = "Submit"/>//simple submit for testing
<div class="grd-button1" onClick="document.forms['submit-form'].submit();"></div>
</form></div>
</div>
</div>
Receiving page,
<?php
$p_OwnerEmaili=$_POST["OwnerEmaili"];
$p_OwnerPasswordi=$_POST["OwnerPasswordi"];
echo "$p_OwnerEmaili;$p_OwnerPasswordi";
Only shows the ;.
Is javascript required to submit from inside a div?

You're accessing the wrong items.
You'll need to set your forms input name attributes to this if you want to access them the way you currently have in your php script:
<input id="OwnerEmaili" type="text" placeholder="User Name" name="OwnerEmaili">
And
<input id="OwnerPasswordi" type="password" placeholder="Password" name="OwnerPasswordi">
That will allow you to access them as you do in your PHP script.
You can always check what values have been sent to your php script by using var_dump() or print_r().
<?php print_r($_POST); ?>
Would've shown you that you had UserName & Password set instead of what you wanted.
As Ghost pointed out in the comments, your form will always send user input via GET if you dont specify a method in it. So set this in your form tag:
<form action="entrance2.php" method="post">

Related

HTML form post method isn't functioning

It's probably something incredibly simple, however I feel like my brain is fried from staring at a computer screen for so long, so any pointers would be appreciated...
I cannot seem to get $_POST to work on my form. When I switch the method to GET - it works fine, but I don't want to have to repeatedly pull form data from the URL as it's quite a lengthy form and would take extra steps etc.
My form data:
<form action="<?= site_url() ?>shop/mock/form/<?= esc($first['gtin']); ?>" name="myForm" method="post">
<div class="p-3 p-lg-5 border">
<div class="form-group row">
<div class="col-md-12">
<label for="brand_name" class="text-black">Brand Name </label>
<input type="text" value="<?= esc($_POST['brand_name']); ?>" class="form-control" id="brand_name" name="brand_name">
</div>
</div>
<?php if (isset($_POST['brand_name'])): ?>
<div class="form-group row">
<div class="col-md-12">
<label for="subbrand_name" class="text-black">Subbrand Name </label>
<input type="email" value="<?= esc($_POST['subbrand_name']); ?>" class="form-control" id="subbrand_name" name="subbrand_name" placeholder="">
</div>
</div>
<?php endif; ?>
</div>
</form>
The only thing I can think of is that I'm sending the form back to the same address everytime, and trying to grab the $_POST data from the latest submission, and repopulate the input values so that the form builds with every submission and reveals the next input etc. var_dump($_POST) shows an empty array every time.
Where am I going wrong?
If you want to receive otp in the form then action empty, if you want to receive from other action, you must put the value into $_SESSION

prevent double submitting into database

Good day all,
I have a question I looked it up everywhere and I still seem stuck
If I submit a form via post -> variable -> database and then redirect to another
page using header(location:page.php); in the time it takes to redirect and I
click on the submit button it still inserts duplicate data into the database
how do I prevent that from happening? as I read on tokens but I can't see how it
will work without processing my form data on another page and I don't want to
do that any help please ??
please see below html :
<form name="frmall"class="col s12" action="" method="post">
<!--basic info------------------------------------------------------------------------------------------------------------->
<div class="divider"></div>
<div class="section">
<h5>Basic Member Information</h5>
<div class="row">
<div class="input-field col s6">
<input id="first_name" name="txt_name" type="text" class="validate" value="<?php if(isset($_POST['btnContinue'])){echo $_POST['txt_name']; } ?>">
<label for="first_name">First Name</label>
</div>
<div class="input-field col s6">
<input id="last_name" type="text" name="txt_surname" class="validate"value="<?php if(isset($_POST['btnContinue'])){echo $_POST['txt_surname']; } ?>">
<label for="last_name">Last Name</label>
</div>
<div class="input-field col s6">
<input id="icon_telephone" type="tel" name="txt_number" class="validate"value="<?php if(isset($_POST['btnContinue'])){echo $_POST['txt_number']; } ?>">
<label>Telephone</label>
</div>
<div class="input-field col s6">
<input id="email" type="email" name="txt_email" class="validate"value="<?php if(isset($_POST['btnContinue'])){echo $_POST['txt_email']; } ?>">
<label for="email" data-error="wrong" data-success="right">Email</label>
</div>
<div class="input-field col s6">
<input id="idnumber" type="text" name ="txt_idnumber" class="validate"value="<?php if(isset($_POST['btnContinue'])){echo $_POST['txt_idnumber']; } ?>">
<label>ID Number</label>
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" name="btnCancel">Cancel</button>
<button class="btn waves-effect waves-light" type="submit" name="btnContinue">Continue</button>
</div>
</Form>
PHP (please not its just for en example):
if (isset($_POST['btnContinue'])) {
$Conn->insert_main_member($name, $surname, $number, $idnumber, $email);
header(location:page.php);
}
So how do I prevent double submit without disabling the button ? please assist
Clear the post variable and place the header(...) in the right place (right after the db query execution).
The question is not very clear...
If you have problems with submitting the form multiple times and inserting the same values, you can do a check - either in your script (connect to database and check) or create a UNIQUE index in your database table - then the record will not be inserted, if it already exists.
If you have problems with too many people on the site and some data is inserted while it shouldn't and it should wait until something else is completed, then you can use LOCK TABLES command to send to your database.
If you have problems with uniquely identifying transactions, you can also use a token (e.g. a random hash generated before your form is submitted), for example as a hidden form field to send along with the data.
1st quick solution :
You could disable your submit button with javascript after 1st form submit to prevent any other submit before redirection...
2nd solution :
Add a token (generated server side) to your HTML form in a hidden input.
On form submit your token will be sent with other POST data.
Check your token server side before insert data into your database
Edit: Perhaps a sample of your code (server side + html form) could help us to give you a more appropriate answer.

Serialize array & put results into hidden text field?

I'm currently encountering a problem which is baffling me. There will be a lack of PHP mixed with HTML due to using a MVC Framework.
What i've got:
Form A on Page 1 with a submit button which directs user input from Form A to page 2 with a chunk of text for the user to read before continuing, a hidden text field which will contain a serialized array from Form A and a submit button to continue to the validation of user input.
My forms direct to the correct pages as required, the post array can be seralized providing I do not put it into a text field. The HTML from form A follows:
<form action="/RegisterInformation" method="POST">
<div class="row">
<div class="large-12 columns">
<input type="text" name="Username" placeholder="Username, ex: JohnDoe ">
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="password" name="Password" placeholder="Password">
</div>
<div class="large-4 columns">
<input type="password" name="cpassword" placeholder="Confirm Password">
</div>
<div class="large-4 columns">
<input type="text" name="email" placeholder="Email, ex: JohnDoe#provider.com">
</div>
</div>
<div class="row">
<div class="large-12 columns">
<textarea name="Referral" placeholder="Where Did You Hear About Us?"></textarea>
</div>
</div>
<div class="row">
<div class="large-4 columns">
<input type="submit" name="submit" value="Continue" class="button">
</div>
</div>
</form>
The redirect to page 2:
<div class="row">
<div class=" large-12 columns">
<p>
<?php echo $data['PreregisterInformation']; ?>
</p>
</div>
</div>
<div class="row">
<div class="large-12 columns">
<form action="/Continue" method="POST">
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
<input type="submit" name="submit" value="Continue" class="button">
</form>
</div>
</div>
With the current example, i've got the text field set to being visible, so I can see what's going on.. But this is where it gets strange.
What i'm getting is two different types of results.
The first, is that when I echo the serialized array outside a input field. I get the desired results:
a:6:{s:8:"Username";s:1:"s";s:8:"Password";s:1:"4";s:9:"cpassword";s:1:"4";s:5:"email";s:3:"asd";s:8:"Referral";s:3:"asd";s:6:"submit";s:8:"Continue";}
The second is when I use the following:
<input type="text" name="SubmitInfo" value="<?php echo serialize($_POST); ?>">
Which passes an invalid serialized array to page 3 breaking the whole process.
I assume it's not a HTML related error, but the new line break in the serialized array definition a:6. I have made an attempt to rectify this problem by removing the new line break with str_replace:
$Ser = serialize($_POST);
$Ser = nl2br($Ser);
$Ser = str_replace("<br>","00",$Ser);
echo $Ser;
which has proven not to work.
This is not a direct solution of your problem, but have you tried json_encode or avoiding the hidden field by storing the date into $_SESSION?

PHP Multiple GET methods in one your

I'm making a tool to pull eve API data and I use a GET form to take the data and get access to the key ID and code to access the data. This then pulls the first character ID on the key and grabs all that character data.
for example the url is:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL
I pull these variables out via the session variables that they create.
What I'm now trying to do is now have a for loop which puts up buttons for each character on the key so you can flick between characters, I'm trying and have tried get and post methods hidden in the button but can't seem to get my desired result. It seems to not be taking out the variable that i'm trying to pass and adding it into the $_SESSION array or the $_POST or the $_GET - i've tried print_r on all these variables but I can't get any output.
Here is my primary form thats on my main page:
<form method="get" action="APIChecker/" >
<legend> Submit an API Key</legend>
<div class="control-group">
<label class="control-label">Key ID:</label>
<div class="controls">
<input type="text"
class="input-small"
name="keyID""/>
</div>
</div>
<div class="control-group">
<label class="control-label">Verification Code:</label>
<div class="controls">
<input type="text"
class="input-xxlarge"
name="vCode"
/>
<p class="help-block"></p>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
What I'm trying to do now is get another session variable from another page - which updates a variable and refreshes the page with the keyID and code unaltered.
As in:
whatever.com/APIChecker/?keyID=123456&code=ABCDEFGHIJKL **&charID=654321**
so $_SESSION['keyID'] = 123456
& $_SESSION['code'] = ABCDEFGHIJKL
& $_SESSION['charID'] = 654321
what I've tried is:
<form method="get" action="" >
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">
<input type="hidden"
name="charID"
value="$charID"
/>
</button>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit API</button>
</div>
</div>
</form>
You can't output PHP values directly in HTML like this: value="$charID"
Try this instead:
value="<?php echo $charID; ?>"

Using "action" in Twitter Boostrap classes

I'm a newbie programmer trying to utilize Twitter Bootstrap to build out a concept. I'm using PHP and assigning actions within HTML tags to POST data and essentially take a user through the navigation. This has been pretty straightforward when using forms: i.e. here is a snippet that does work. For example, for this snippet of HTML:
<form action="?viewnotes" method="post">
<input type="hidden" name="id" value="<?php htmlout($note['id']); ?>">
</form>
It successfully triggers the following if statement in my index.php:
if (isset($_GET['viewnotes']))
However, I'm trying to do the same thing in my registration page, using a Twitter Bootstrap class for buttons. Here is the HTML:
<a class="btn btn-primary btn-large" action="?register">Sign Up Free!ยป</a></p>
The PHP code is:
if (isset($_GET['register']))
{
include 'register.html.php';
}
Clicking on the Sign Up button is not invoking the PHP code. If I hard code the URL it works, but then I have a similar issue on the register.html.php page. HTML on the register page has:
<form class="form-horizontal" action="?storeuser" method="post">
<fieldset>
<legend>It's quick and easy...</legend>
<div class="control-group">
<label class="control-label">First Name</label>
<div class="controls">
<input type="text" name="fname" class="input-xlarge" id="fname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="lname">Last Name</label>
<div class="controls">
<input type="text" name="lname" class="input-xlarge" id="lname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="email">Email Address</label>
<div class="controls">
<input type="text" name="email" class="input-xlarge" id="email">
</div>
</div>
<div class="control-group">
<label class="control-label" name="password">Password</label>
<div class="controls">
<input type="password" name="password" class="input-xlarge" id="password">
</div>
</div>
</fieldset>
<button type="submit" name="action" class="btn btn-primary btn-large">Complete Registration</button>
</form>
However, when clicking on the button, the index file does not trigger the following, which would store the fields into the DB.
if (isset($_GET['storeuser']))
If I hardcode the URL to register.html.php, then the resulting URL looks like localhost/register.html.php?storeuser instead of localhost/?storeuser. I'm not sure if that's affecting the behavior here.
Thank you for the help!
I think you're approaching this the wrong way, and it's not Twitter Bootstrap's fault.
Usually, you'd use POST, not GET, to handle user registrations. Your form would look like this:
<form action="register.php" method="post">
<!-- your form -->
<fieldset>
<input type="submit" name="register" value="Register" class="btn btn-primary" />
</fieldset>
</form>
You can then build register.php as follows:
<?php
if (isset($_POST['register'])) {
// handle user registration
}
// display form
?>
This will display the form when the user visits register.php, but will try and process the user registration first if the form's been POSTed.
try passing a value with your GET variable
<form action="?viewnotes=1" method="post">

Categories