PHP Form with process on same page? - php

I've created a form on my page and from the tutorials im following it says I have to have a second page with all the php processing in, is it not possible to keep the php on the same page and when the user hits submit the form is sent?
Why isnt this working on my process page? It doesnt echo anything :S
<?php
$kop = mysql_real_escape_string($_POST['severity']);
$loc = mysql_real_escape_string($_POST['location']};
$summary = mysql_real_escape_string($_POST['summary']);
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
echo $kop;
echo $loc;
echo $summary;
echo $name;
echo $email;
?>

You can check in the same file if the submit button is pressed. (http://pastebin.com/8FC1fFaf)
if (isset($_POST['submit']))
{
// Process Form
}
else
{
// Show Form
}
Regarding form checking, you can save the user input, and if one of their data is unvalid you can echo the old data back and show the form again.
edit: As PeeHaa stated, you need to leave the action blank though ;)

Yes it is possible. As adviced by Pekka, it's not really suggested to do so. But here is how it can be done.
Simply using the isset method to check if the form has been posted. So say in your form you have the follwing input:
<input type="text" name="age" />
At the top of your php script you can use the following to know if the form has been submitted and thus process it.
<?php if(isset($_POST["age"])) {
//do processing
} ?>
Hope this helps ;)

It is possible to let the same script process the form.
If you want to do this just leave the action blank.
However If you want to process the form without the page being reloaded you have to use Javascript.

is it not possible to keep the php on the same page and when the user hits submit the form is sent?
It sounds like you are describing AJAX. Example: http://www.elated.com/res/File/articles/development/javascript/jquery/slick-ajax-contact-form-jquery-php/
This is using the jQuery framework - there are a number of frameworks for doing this (such as YUI) which could do this equally as well. You could write this yourself to learn how it all works, but IHMO this would be reinventing the wheel. Here is a decent tutorial on creating AJAX forms with jQuery:
http://www.elated.com/articles/slick-ajax-contact-form-jquery-php/

<form name="myfrom" action="" method="post">
Username: <input type="text" name="user" id="username" />
<input type="submit" name="submit_form" value="Submit" />
</form>
<?php if($_POST['submit_form'] == "Submit"){
echo "do something";
}
?>

Related

Form input fields validation via jQuery and PHP

I want to validate my form input fields on button click in jQuery. But I don't want to use client side validation. How can I add PHP server-side validation on button click?
I am using modal box for form.
I know it is a beginner's level question, but currently I am unable to figure it out because of my low expertise.
Yes you can validate using PHP and it's not a big deal. I'll provide a simple example here to pass the form data to PHP for validation.
<?php
if( isset($_POST['submitForm']) )
{
$userName = $_POST['userName'];
$userAge = $_POST['userAge'];
if ($userAge > 21)
{
echo "Hey " + userName;
}
}
?>
<html>
<body>
<form method="POST" action="">
Name: <input type="text" name="userName">
Age: <input type="number" name="userAge">
<input type="submit" name="submitForm" value="Validate">
</form>
</body>
</html>
If you wish to pass the data to another page to validate, just give the file name in action attribute, like action="Your_PHP_File.php" and validate separately. Just a tip: If you wish to redirect on success (heavily used):
header("Location: Your_URL_Here");
Anyways, this is not a better user experience. You must use client side validation using JavaScript/any JavaScript framework, and use language like PHP only to communicate with the server, such as storing the data in the database, retrieving data from the database.

PHP and html post button value

I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.
<form action="postphp.php" method="post" enctype="multipart/form-data">
<center><input id="postTitleStyle" type="text" name="title" size="100" maxlength = "180" ></center>
<center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
<center><input id="postTagStyle" type="text" name="tags" size="100" maxlength = "900" ></center>
<center><input type="submit" class = "Post2" value="post"></center>
</form>
Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?
P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.
Many thanks, Jack.
There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.
Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">
A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.
Solution 2:
In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.
E.g.
<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>
In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.
Hopefully this helps a bit.
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];
mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
$id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.

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."
}
?>

How can I execute php before posting to someone elses script from a form?

I have a contact form which posts to mailchimp - but in certain cases, I also want it to send out an email.
I considered changing the <form action="... from mailchimp to my own page, containing something similar to the following:
<form action="mailchimp_url..." ...>
<? foreach($_POST as $name=>$value){?>
<input type="hidden" name="<? echo $name;?>" value="<? echo $value;?>">
<? }?>
<? //mail the stuff I want somewhere else ?>
I could then just auto execute this on pageload with javaScript.
The Problem is, IF this works, it will be dependant on JavaScript, or will require an additional button click for the user.
Is there a more elegant way to do this?
You can post to your own server first
<form action="your_path" ...>
Then use cURL to post the same data to mailchimp
Here is a good example on how to
this code will be done if you come from another form, submited.
Then your $_POST will be parse. But because $_POST exists only if a form is posted, you can't use it before.
What do you want to do exactly? If you want to add hidden input in your form fontion of specific params, you may use $_GET or maybe $_SESSION

Form refresh after submit

The form below can contains different elements of text fields, drop down and selection boxes, which allows the user to update his profile. The process of updating MySQL fields is being done after form submits.
<form method="post">
My Name: <input name="myname" type="text" value="<?php echo $_SESSION['SESS_MY_NAME']; ?>" /><br />
My Email: <input name="email" type="text" value="<?php echo $_SESSION['SESS_EMAIL']; ?>" /><br />
<input type="submit" name="Submit" value="Update" />
<?php
if (isset($_POST['Submit'])) {
// MySQL update
;
;
;
$result=mysql_query($sql);
}
// if successfully updated, make form refresh.
if($result){
;
;
;
}
?>
</form>
I want to refresh only the form, so that the user will stay in the same page and will see the changes that he made (i.e. What do I need to put under the comment in the code “if successfully updated, make form refresh”?).
I cannot use header("location: samepage.php"); because I have too many HTML codes involved in between and before.
Appreciate any help,
Move the isset($_POST['submit']) check to the top.
Your current (intended) process is as follows:
Retrieve form data from DB/Session/etc.
Display as HTML
User submits form
Repeat steps 1/2
Write data to DB
Force refresh
Repeat 1/2 again
If you move this check to the top, the process is changed to:
Retrieve form data from DB
Display as HTML
User submits form
Write data to DB
Repeat 1/2 (it will now retrieve the updated information and display correctly).
This is the absolute simplest way you can do it. It doesn't take into account Post/Redirect/Get or updating the session so multiple DB reads are unncessary, etc.
Why don't you use jQuery AJAX to submit and verify the form then output the result in chosen div element.
More on that available at nettuts+
Such helpful. Much wow.
Thankfully I was able to solve this on my own after some intense googling since no one else seemed to have an answer. For anyone looking to accomplish the same thing, all you have to do is add the following code to the "Additional Settings" section of your CF7 form configuration:
on_sent_ok: "location = 'http://domain.com/contact';"
Just replace the URL with the URL of your form page and presto, it'll automatically refresh back to that page when the form is submitted!

Categories