Jump to content

[SOLVED] Forms - Submit


EagerWolf

Recommended Posts

Hello!

 

I have got form.php, which generates forms, and in the same file I need to put few lines which are excecuded if form is submitted.

 

so form action must be <?php echo $_SERVER['PHP_SELF'] ?> ...

 

 

How do I create if statement then?

 

Thanks for yout help!

 

Best regards,

M

Link to comment
https://forums.phpfreaks.com/topic/38792-solved-forms-submit/
Share on other sites

it's very simple. there are a few ways to do it, it really depends on what your page looks like and what you've got going on.

 

example:

<?php
       if(isset($_POST)){
                /*execute code here*/
                unset($_POST);
        }
?>

Link to comment
https://forums.phpfreaks.com/topic/38792-solved-forms-submit/#findComment-186590
Share on other sites

Here is some code i wrote just testing out everything:

 

<?php

$email = "email";
$ne = "no email";

echo "<table align=\"center\" width=\"50%\" height=\"10%\">";
echo "<tr>";
echo "<form ACTION=\"" . $_SERVER['PHP_SELF'] . "\" METHOD=\"POST\">";
echo "<td align=\"center\" width=\"100%\">";
echo "<INPUT TYPE=\"text\" NAME=\"email\">";
echo "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Send\">";
echo "</td>";
echo "</form>";
echo "</tr>";
echo "<tr>";
echo "<td align=\"center\" width=\"100%\">";

if (isset($_POST['email']) && $_POST['email'] == $email) {

	echo "EMAIL!";

}
elseif (isset($_POST['email']) && $_POST['email'] == $ne) {

	echo "NO EMAIL";

}
else {

	echo "";

}
             ?>

 

in this if i type "email" in the text area and hit submit "EMAIL shows up under it. If i type "no email" then NO EMAIL shows up under it. If i type anything other than those 2 nothing shows up hence the echo " ";. I hope this helps as far as your forms and $_SERVER['PHP_SELF'];

 

Also if you don't want a script run until the user clicks submit then you can do:

 

if (isset($_POST['submit'])) {

  //put the info here that the form redirects to, in this case action=\"$_SERVER['PHP_SELF']\"

}
else {

  echo "Please fill out the form and click Submit.";

}

that way they can't just go to the page that pulls info or inserts info into the DB from the form. Figured i would throw that in. Just a small security messure.  I hope this helps.


Link to comment
https://forums.phpfreaks.com/topic/38792-solved-forms-submit/#findComment-186601
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.