Trying to test my post form (debugging) - php

I have a form, and I'm checking to see if it's submitting properly to post. This is my first foray into POST, so other than rudimentary research and tutorial stuff, I'm having a lot of problems. I wrote a simple script to see if my form is working properly at all. The HTML is clipped to only show you the form; I do have a validation and all that.
There is no naming conflict, whether in the filenames or those of the variables, so I assume it's a syntax error or just me being that guy with no knowledge of post whatsoever.
Here's the HTML:
<html>
<body>
<form name="Involved" method="post" action="postest.php" target="_blank">
Name: <br><input type="text" name="name" title="Your full name" style="color:#000" placeholder="Enter full name"/>
<br><br>
Email: <br><input type="text" name="email" title="Your email address" style="color:#000" placeholder="Enter email address"/>
<br><br>
How you can help: <br><textarea cols="18" rows="3" name="help" title="Service you want to provide" style="color:#000" placeholder="Please let us know of any ways you may be of assistance"></textarea>
<br><br>
<input type="submit" value="Submit" id=submitbox"/>
</form>
</body>
<html>
Here's the post (named postest):
<?php
$name = $_POSTEST['name'];
$email = $_POSTEST['email'];
$help = $_POSTEST['help'];
echo {$name}, {$email}, {$help};
?>
This post was derived from this tutorial.
Also, I might as well ask: How would I go about submitting information to be (semi)permanently stored on a spreadsheet for my later perusal? However, this is a secondary question.

Part of your problem is that you are using a variable you're calling $_POSTEST when really what you want is the $_POST array. $_POST is a special reserved variable in PHP (and needs to be referenced using that exact syntax) which is:
An associative array of variables passed to the current script via the
HTTP POST method.
Reference: PHP Manual - http://php.net/manual/en/reserved.variables.post.php
So whatever input names and values you're passing into the PHP script come in via HTTP POST, and they'll be located in the $_POST array.
So using your example, it would be:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
echo {$name}, {$email}, {$help};
?>

There is not any $_POSTTEST array in php.
Use $_POST.

Related

Validating information in HTML - Code Positioning

I am having trouble getting my code validation to work. I have written validation for a name, surname and email address, however, I don't know where to insert a command for the php code to be called in my main html.
I was thinking I have to add an action into a form like this:
<body>
<div class="logo"></div>
<div class="login-block">
<h1>Create Account</h1>
<form action="insert_data.php" method="post">
<form action="validate_data.php">
<input type="text" value="" placeholder="First Name" name="first_name" />
<input type="text" value="" placeholder="Last Name" name="last_name" />
<input type="email" value="" placeholder="E-mail Address" name="email_address" />
However, I don't know if that is correct. All three of the validation notes are saved in a file called 'validate_data.php'.
My code for name and surname validation is pretty much the same, with the main 'name' spaces changed:
<?php
$first_name = test_input($_POST["first_name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
$first_nameErr = "Incorrect name format.";
}
?>
and for my email:
<?php
$email_address = test_input($_POST["email_address"]);
if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
$email_addressErr = "Invalid email format.";
}
?>
Is there any particular place I will have to call this? Or am I just doing some stupid mistake and missing it?
You don't put it in the HTML unless you are sending the page to itself, in which case it is usually best to put the PHP at the top of the page. It would need to be named as a .php page not .html then. That way if, for example, you wanted to make the values of the form stay as what was submitted you could echo them after they are cleaned up by setting the textbox value
value="<?php echo $first_name; ?>"
for example. If you are submitting to insert_data.php all the PHP just lives on that page at the top. You seem to have too many <form actions - you can only submit once. Best to put your cleanup code at the top of insert_data.php and submit to that.
If it is on the same page you would need to wrap it in
if( isset($_POST["first_name"])){
// do the cleanup
}
or you will get messages for the empty inputs which have not had the chance to be submitted as the page loads. And do the same for the email address which you definitely don't want to have blank if you are subsequently going to use it for a mail form's Reply-To: address (the From: should always be an address on your server or you will be back here posting wondering why it doesn't work!)
You could include the validation script but that is possibly a bit risky and for the length of the code there is probably little advantage in having it as an include. Risks of including unsafely: http://www.webhostingtalk.com/showthread.php?t=199419
You would then use - assuming your includes are in a folder called inc/
include "inc/validate_data.php";
at the top of your insert_data.php page - without the brackets shown in that article - it is a declaration, not a function.
Another good article on includes:
http://green-beast.com/blog/?p=144
If you were looping out posts, for example, the code to do that would be somewhere among the HTML inside the div where you wanted them to appear.

Why won't the results of my form POST to another .php?

I've got a real simple html form:
<form action="emailform.php" method=post>
<textarea name="emailBody"></textarea>
<p>If you want a reply :</p>
<input type="text" name="userEmail" id="emailSubmit" placeholder="Your Email">
<br>
<input type="submit" name="submit" class="submitButton">
</form>
And I'm just trying to get the inputs assigned to their respective variables in 'emailform.php', like this:
<?php
$email=$_REQUEST("userEmail");
print $email;
?>
As you can see I just tried it with one of the inputs, because I wasn't sure if the textarea works the same as a regular input, but even '$userEmail' doesn't seem to be getting the info as it doesn't print or echo anything.
I'm fairly new to this so this particular exercise is mostly for practice. In the end I don't want the submit button to redirect to another page, and I want the inputs emailed somewhere automatically, so if you can explain how to do that that would be great!
Your syntax is incorrect. You use brackets, not parentheses, to access array values.
$email=$_REQUEST("userEmail");
should be:
$email=$_REQUEST["userEmail"];
You could possibly try the following, if you are trying to assign what the end user has input into the field.
You have put:
$email = $_REQUEST("userEmail");
I personally believe this would not be the best way to do this as you are using the PHP POST method in your form therefore you should use the POST method in your PHP code also like this:
$email = $_POST['userEmail'];
$_REQUEST can be used for both $_POST and $_GET. But your are posting your information but if your are GETting the users input then simply use $_GET
Try that and tell me if it works.
After adding the missing the quotes on method="post" in your form tag, try this:
<?php
if (isset($_POST['submit'])) {
$email = $_POST['userEmail'];
print $email;
} else {
print "Oops, something went wrong."
}
?>

Posting the datas through URL

I like to pass the values to the webpage. I am having the array in which the values are stored dynamically. I like to pass this array to webpage through URL, website is in PHP Language. I am not aware of the variables present inside the PHP page.Did we have any types of technique or script to get the variable names which used in PHP code from the client side.
using method=get and $_Get method ,
Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send.
example
<form action="welcome.php" method="get">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" value="Submit"/>
</form>
Welcome.php(get the value from url)
<?php
echo $_GET["fname"];
echo $_GET["age"];
?>

Storing form data with PHP Session

I am working on an HTML form whose data would be saved on a failed submit, or page refresh.
We are using a PHP Session to store this data, and we post the elements back when needed.
The issue is that it's not working. We need the form data to be preserved on a submit with errors, or page refresh. Currently on a failed submit, or page refresh, there is no data being stored in the session.
I'm fairly new to PHP, and most of this code is not mine, so go easy on me.
The PHP Sumbit code being used is:
Software: PHPMailer - PHP email class
Version: 5.0.2
Contact: via sourceforge.net support pages (also www.codeworxtech.com)
Info: http://phpmailer.sourceforge.net
Support: http://sourceforge.net/projects/phpmailer/
SESSION:
<?php
session_name("fancyform");
session_start();
$str='';
if($_SESSION['errStr'])
{
$str='<div class="error">'.$_SESSION['errStr'].'</div>';
unset($_SESSION['errStr']);
}
$success='';
if($_SESSION['sent'])
{
$success='<div class="message-sent"><p>Message was sent successfully. Thank you! </p></div>';
$css='<style type="text/css">#contact-form{display:none;}</style>';
unset($_SESSION['sent']);
}
?>
FORM:
<form id="contact-form" name="contact-form" method="post" action="submit.php">
<p><input type="text" name="name" id="name" class="validate[required]" placeholder="Enter your first and last name here" value="<?=$_SESSION['post']['name']?>" /></p>
<p><input type="text" name="email" id="email" class="validate[required,custom[email]]" placeholder="Enter your email address here" value="<?=$_SESSION['post']['email']?>" /></p>
<p><input type="text" name="phone" id="phone" placeholder="Enter your phone number here" value="<?=$_SESSION['post']['phone']?>" /></p>
<p>
<select name="subject" id="subject">
<option>What event service are you primarily interested in?</option>
<option>Event Creation</option>
<option>Project Management</option>
<option>Promotion</option>
</select>
</p>
<textarea name="message" id="message" class="validate[required]" placeholder="Please include some details about your project or event..."><?=$_SESSION['post']['message']?> </textarea>
<input type="submit" value="" />
</form>
You are outputting $_SESSION['post']['form_element'] to your template but in the above PHP code there is no mention of setting that data. For this to work, you would have to loop through the $_POST array and assign each key pair to $_SESSION['post']
At that point you should be able to access the previously submitted form data from the session just as you have coded above.
add this to your submit.php:
session_start();
foreach ($_POST AS $key => $value) {
$_SESSION['post'][$key] = $value;
}
This will move all the data from $_POST to $_SESSION['post'] for future use in the template, and should be all you need to get it working.
HTTP is stateless, and data that is not submitted will not remain unless you use a client-side approach (webstorage, etc).
You need to parse the post variables and add them to the session super global, right now you are referencing $_SESSION['post']['phone'] which won't work as you expect.
// Assuming same file, this is session section
if (array_key_exists('submit', $_REQUEST)) {
if (array_key_exists('phone', $_REQUEST)) {
$_SESSION['phone'] = $_REQUEST['phone'];
}
}
// Form section
<?php $phone = (array_key_exists('phone', $_SESSION)) ? $_SESSION['phone'] : null;?>
<input type="text" name="phone" id="phone" placeholder="Enter your phone number here" value="<?php echo $phone ?>" />
Either you didn't include the code for submit.php or, if you did, the problem is clear: you're never setting the session values. In order to do that, you'd use
session_start();
// etc...
$_SESSION['post']['name'] = $_POST['name'];
$_SESSION['post']['email'] = $_POST['email'];
$_SESSION['post']['phone'] = $_POST['phone'];
// etc...
on submit.php and then the form page (presumably another page?) could then check if those values have been set
.. value="<?php if (isset($_SESSION['post']['name'])) echo $_SESSION['post']['name']; ?>" ..
I may be wrong, but I think you need to get the posted values from the form by using something like
if ($_POST['errStr']) {
$_SESSION['errStr'] = $POST['errStr'];
}
If i'm right its the way you're trying to access the variables after posting the form
If you look at the METHOD attribute of the form it set as post, so this should return the values you want to pass accross pages.
Maybe this isn't what you were asking though, i'm a little unclear what part the problem is with, i'm assuming its taking values from the form and getting them out on the next page.
If you want to do it when the page is refreshed/exited you'd probably need to use some javascript client side to try and catch it before the action happens.
Don't know if this is possible. PHP wont help you for that as its executed server-side, and the client wont submit anything (useful) to the server when they exit/reload, only the command to perform the action.
This'll probably require using javascript listeners, eg window.onclose (although apparently that doesn't work for safari or firefox 2), and within that an ajax xmlhttprequest to send the data to your server.
For failed submit (ie capture form with invalid data in?) its almost the same case as form that submit worked on. Just re-check the data on the other side when you're processing it.

What is the purpose of $_POST?

I know it is php global variable but I'm not sure, what it do?
I also read from official php site, but did not understand.
You may want to read up on the basics of PHP. Try reading some starter tutorials.
$_POST is a variable used to grab data sent through a web form.
Here's a simple page describing $_POST and how to use it from W3Schools: PHP $_POST Function
Basically:
Use HTML like this on your first page:
<form action="submit.php" method="post">
Email: <input type="text" name="emailaddress" /> <input type="submit" value="Subscribe" />
</form>
Then on submit.php use something like this:
<?
echo "You subscribed with the email address:";
echo $_POST['emailaddress'];
?>
There are generally 2 ways of sending an HTTP request to a server:
GET
POST
Say you have a <form> on a page.
<form method="post">
<input type="text" name="yourName" />
<input type="submit" />
</form>
Notice the "method" attribute of the form is set to "post". So in the PHP script that receives this HTTP request, $_POST[ 'yourName' ] will have the value when this form is submitted.
If you had used the GET method in your form:
<form method="get">
<input type="text" name="yourName" />
<input type="submit" />
</form>
Then $_GET['yourName'] will have the value sent in by the form.
$_REQUEST['yourName'] contains all the variables that were posted, whether they were sent by GET or POST.
It's used to store CGI input via a POST sent to your page.
Example:
Your page contains:
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
One the user submits the values input into the form, you can access those variables through $_POST using the names you provided for the input tags.
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
You can capture post values from forms:
Example:
<form method="POST">
<input type="text" name="txtName" value="Test" />
</form>
To get this you'll use:
$_POST["txtName"];
It contains data sent by HTTP post, this is most often from a HTML FORM.
<form action="page.php" method="post">
<input type="text" name="email" ...>
...
</form>
Will be accessible by
$_POST["email"]
It contains the data submitted via the POST method, and only the POST method, versus data submitted via the GET method. The $_REQUEST superglobal variable contains both $_POST and $_GET data.
When data is posted through a form to the server, you access it through the $_POST array:
<form method="post">
<p><input type="text" name="firstname" /></p>
<p><input type="submit" /></p>
</form>
--
<?php
if ($_POST)
print $_POST["name"];
?>
Not all data is sent through $_POST through. File uploads are done through $_FILES.
As defined by the Hypertext Transfer Protocol specifications, there are several types of requests that a client (web browser) can make to a resource (web server).
The two most common types of web requests are GET and POST. PHP automatically loads any client request data into the global arrays, $_GET and $_POST, based on the type of web request received. The type of request is transparent to the user of the web browser, and is simply based on what is going on in the page. In general however, any regular link you click produces a GET request, and any form you submit produces as POST request.
If you click a link that goes to "http://example.com/index.php?x=123&y=789", then index.php will have it's $_GET array populated with $_GET['x'] = '123' and $_GET['y'] = '789'.
If you submit a form that has the following structure:
<form action="http://example.com/index.php" method="post">
<input type="text" name="x">
</form>
Then the receiving script, index.php, will have it's $_POST array populated with $_POST['x'] = 'whatever you typed into the textbox named x';
There are two ways of sending data from a form to a web app, GET and POST.
GET sends the data as part of the URL string: http://www.example.com/get.html?fred=1&sam=2 is an example of what that would look like. There are some problems with using it for all processing, one of the biggest is that every browser has a different maximum length for the query string, so you may have your data truncated.
POST sends them separately from the URL. You avoid the short length limit, plus you can send binary or encrypted data with POST.
In the first example above, PHP can retrieve the values sent by $_GET['fred'] and $_GET['sam']. You would use $_POST instead if the form was POSTed.
If you're wondering which method you should use, start here
$_POST is used to retrieve values passed to your page via a POST request.
For example, your page uses a form to pass data to another page in your application. Your form would have
<form method="post">
to pass those values via POST.
It is matched by $_GET which perform the same function for GET requests.
If you want to be able to reference either GET/POST values, you can use $_REQUEST
It contains any values posted from a HTML form to this script.

Categories