$_SESSION variable used to check if form has been submitted - php

I have a landing page called `index.php' with the following form:
<form action="auto_mail.php" method="post">
<input id="signup" class="span8" type="text" placeholder="Your email" name="signup">
<input type="submit">
<?php
if (isset($_SESSION['got_it']))
{echo "<b>You're all signed up!</b>}
?></form>
In the file auto_mail.php I have:
// code scrubbing user input...
$user_email = $_POST['signup'];
session_start();
$_SESSION['got_it'] = '1';
// code sending me an email when someone signs up.
echo <<<EOD
</b>
<meta http-equiv="refresh" content="0, url=index.php">
</div>
</body>
</html>
EOD;
?>
I've looked at some other SO questions (Using $_SESSION to carry data), but it's not what I'm looking for.
All I want, is for a user to see "You're all signed up" when they enter a valid email; with the email confirm email being sent in the background. This code feels clumsy and awkward. It also flashes the auto_mail.php page briefly.
I tried to set <form action="index.php"..., but it doesn't work because I've set up auto_mail.php such that you can't access it directly.
How can use the code in auto_mail.php, which checks for a valid email address and sends confirm emails, without dealing with both $_POST and $_SESSION, or at least using them better?

If you don't want to have any page reloads whatsoever, you'll have to use AJAX to send the form, instead of utilising the form POST.
If you are using jQuery, or Mootools, they both have built in wrappers to handle ajax calls. Without a helper library, you'll have to look into making an XMLHttpRequest yourself.
Other than that, traditionally, you would redirect the user to a "form submitted" page, or alternatively, have the form action be sent to the same page (in your case, index.php, and have PHP code to handle form data if it is received).

I dont get completely what you want.
I think you try to Verify a Mail Address (after?) that form has been sent. But you cannot access the file via http that does the verification.
Have you thought about including the auto_mail.php?

I think you should consider using one of popular PHP frameworks. I guess you didn't use any in above example. Good framework that also offers MVC structure allows to do operations like this in such a simple way you can't even imagine.
Breaking it down to MVC structure will even make it extremely simple to handle post sending and displaying dependences and results made by it in one action.
Learing good framework at first might look like a waste of time, but believe me - it will pay off very quickly.
For start I recommend you looking at Kohana Framework or, if you're ambitions one - Symfony Framework.

Related

How to Send Form Data to an External Database and Process It?

In one site, www.example.com, I have a form. It is processed and stored in its database.
Is it possible to send this form data also to another site (www.client-site.com)? It is located on a different server. And this client-site should receive the data and store it on its own database.
I am not sure of the terminology and what should I've been looking for.
Tested different search queries here in SO and this is what most resembles it:
[php] [mysql] +form +"$_post" +external
I'd like to develop this with PHP and the databases run MySQL, and surely security is important in this data transaction.
And hoping this is not a SOAP matter... ;)
Justification:
www.example.com runs a mini site of one of my clients
www.client-site.com is the client main site
being a large company, they cannot provide me credentials to their main site database
I have to propose an alternate solution
You will need to have something like a webservice (this is the word you are looking for) on site b which you can use from the code within your site a.
SOAP is one possibility to create a webservice, but there are many other possibilities. One is shown in this answer on stackoverflow.
NEVER EVER try to archiv this using forms and something like cURL!
Further you should look for proper authorization on your endpoint (site b) and ensure that ssl is used, as security is important to you.
If you have a form hosted on www.example.com like this:
<form method="post" action="http://www.client-site.com/handler.php">
...
Then the page http://www.client-site.com/handler.php will be able to access the post variables.
This is why it is important that you validate your post data in your own PHP applications as you can never be sure where the data is coming from and therefore cannot trust it.
I can think of a very ugly solution wich will do the trick :)
<script language="JavaScript">
function submitForm(theform) {
theform.action = "SITE ONE";
theform.target="myframe1";
theform.submit();
theform.target="";
theform.action = "SITE2";
theform.submit();
}
</script>
<html>
<body>
<form action="" onSubmit="submitForm(this); return false;">
<input type="text" name="userName" value="" />
<input type="Submit" />
</form>
<IFRAME name="myframe1" src="about:blank" width="0"
height="0"></IFRAME>
</body>
</html>
This is not so conventional but will do the trick !
check it out :)

How can i validate if the email is already exist in the database without refreshing the php page?

As of now I am validating my inputs using this type of approach
<form method="post">
<label>Email</label>
<input type="email" name="user_email">
<input type="submit" name="reg">
</form>
if(isset($_POST['reg']){
$result = checkEmail($_POST['email']);
//checkEmail() is my function to check email, if it returns true it has duplicate
if($result){
echo '<p>E-mail already exist!</p>'
else{
//something to do in this..
}
I have seen some website after I type the email it automatically updates and i want to learn how to that without using any frameworks since I am just a starter, i just want a simple code. Any suggestions or advice on how to do it? Thank you :)
You'd have to use Ajax for that. To use Ajax natively is a lot of work though, you'd have to care for different browsers and a lot of ground work which can be taken care of by using a lightweight javascript libraries like jQuery. Using jquery together with an excellent plugin like Validate you can achieve what you're looking for. They have a working example of what you're trying to do at this demo page
I'm sorry but I think you'll need to use some AJAX code, HERE
I found a very interesting code.

HTML5 form with PHP post method

I am trying my hand at creating an HTML5 form styled with CSS3. However, in the past, I have only used form building tools that generate the PHP action code. I have found such varied examples online, that I want to ask here if someone can give me an example of simple, clean and valid PHP for a 'post' method.
I am going to take advantage of the HTML5 'input' types and attributes, so I would like for the PHP to simply take the data (already validated by the browser through HTML5) and send it to me by email and then display a success message on the website for the sender. Thank you.
Edit: What I am asking for is the code for the PHP file that the form will point to for processing. I know how to do the HTML and CSS, but would appreciate an example of the PHP code to complete the actions I mention above.
It seems you are confusing html5 with css3 with php. They are all separate technologies that are used together. A form is just a form. You submit it and your php processes it. You need to read about post and get processing in php. For the html side you just submit a form and specify which of the two methods (get or post) you want to use
<!-- none of this has anything to do with html5 or css3 -->
<!-- for method specify POST or GET -->
<form method="POST" action="blah.php" >
<input type="text" name="foo" value="hello world" />
<input type="submit" value="Submit" />
</form>
//receive the form data in php and do something with it
<?php $blah = $_POST['foo']; //returns hello world ?>
Handling emailing stuff to yourself is whole other can of worms. Get this down first and then ask that part in a new SO question.
Update - based on update to question by OP
The two things you are looking for are pretty simple. First create a php file that the form submits to. In my example above I chose blah.php. There you can query the global variable $_POST[]. This variable will contain a hash for each input name and input value. In the example above these are foo and hello world.
$message .= $_POST['foo'];
You can then use the php mail method to send an email to yourself.
mail('me#site.com', 'Subject', $message);
And finally whatever you echo out will be returned to the user
echo 'this is blah.php. Thank you for submitting a form!';

Passing variables on the same page

The user enters a number and clicks submit. The number now shows up on the page.
The user is then asked if they would like to double the number. They click yes.
The doubled number now appears on the page.
I am having trouble with part 3. Is this possible using just PHP?
UPDATE: Thanks for your answers. This is my first ever PHP script, so I wasn't sure. I am going to research doing it with AJAX just now. I'm very curious to know why it is possible to get to part 2 if you can't get to part 3. Can anyone explain this or provide a link?
<?php
$double = (isset($_REQUEST['do_double']) && $_REQUEST['do_double'] == '1') ? ($_REQUEST['number'] * 2) : '';
?>
<form method="get" action="?">
<input type="hidden" name="do_double" value="<?php echo isset($_REQUEST['number']) ?'1' : '0'; ?>" />
<input type="text" name="number" value="<?php echo isset($_REQUEST['number']) ? $_REQUEST['number'] : '';?>" />
<?php if ( ! isset($_REQUEST['number'])) { ?>
<input type="submit" value="submit" />
<?php } else { ?>
<input type="submit" value="Verdoppeln" />
<?php } ?>
</form>
<div id="number"><?php echo $double; ?></div>
I think you're talking about session variables. At the top of the script add the following script to start a session for the current user:
session_start();
This will allow you to store variables in session $_SESSION which persists between requests. Use isset to check if a value is set in session.
Take the form ID using something like document.formname.inputname (http://www.quirksmode.org/js/forms.html) and then multiple by two then use javascript to show it where ever you want.
Asynchronous is required I think. Try these 2 reads:
the difference between synchronous and async: http://javascript.about.com/od/ajax/a/ajaxasyn.htm and
possible examples: http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
This is really more of a comment than an answer, but SO limits comment length so here 'tis:
The misconception that a web page is synonymous with a PHP script appears to be common among novice PHP programmers. Really, a web page is an HTML document (with associated resources) that is sent from a web server to a web browser as part of an HTTP response. A single server-side script can generate many different kinds of web pages, and when processing HTML forms (as you seem to be doing) it is common for a single server-side script to do exactly that. In "web 1.0" applications, clicking on "Submit" (for example) typically causes the browser to make a new HTTP request (called a "page turn"), and the web server keeps track of what the user is doing across page turns by storing "state" either in a "session" (with an associated key included in the HTTP header of each HTTP request - and the HTTP response generated by the web "login") or in one or more HTTP parameters. The point is that each new HTTP request may be for the same script/URL on the server, but the behavior (and the appearance of the web page) will be different because the server is somehow tracking the state of the "workflow" across page turns.
Now if you really must avoid page turns, you can use Javascript to change the appearance/state of the page displayed by the browser without making the user click on a "Submit" button. And the XMLHTTPRequest mechanism (aka AJAX), which allows content to be fetched from the web server "behind the scenes" and change the state of the client-side document without the user doing anything, is typically used to achieve this. But is only really necessary if you're doing something rather different (e.g. scrolling a map or updating a stock ticker) than what you describe in your question, which looks like a perfect example of a "web 1.0" application workflow.

Javascript redirect vs form submit to a php script

I've got a form on my websites homepage that contains a box for a user to enter a members name. From there the form currently submits to a PHP script which just calls Header('Location: blah); and then directs them to /search/username/.
Would it be worth it (i'm assuming so) doing that redirection in javascript so that directs them straight to /search/username? If so how would I go about redirecting with javascript, just plain old window.location = "http://www.google.com/"
Server side is always better method as it's more difficult to bypass than client side method.
Be sure to add exit function after redirection using header.
ob_clean();
header('Location: target.php');
exit();
To do it in JavaScript, you could:
<input id="username" type="text" onclick="" />
<input type="button" onclick="window.location='/search/'+document.getElementById('username').value" />
You could add some validation to the event as well.
Would it be worth it
That depends on your specific situation. If your server is getting 100's of these requests a second, then sure. Otherwise, it really doesn't matter which way you do it.

Categories