Jump to content

PHP Form unwanted refreshing/redirecting


AnimaStone

Recommended Posts

I have an exercise for school that I have to work out. My teacher is letting us figure it out ourselves, but I've ran into some troubles:

 

- We have to make 2 files: sescook1.php and sescook2.php.

- We have to make a form for the user to fill out their name and favorite color (in sescook1.php).

- We have to check the input (whether there is input or no). If there's isn't any input, we have to echo 'Please fill out __' (in sescook1.php)

- We have to store the name in a session variable and the color in a cookievariable (in sescook1.php)

- If both fields are filled in, we ave to show a link 'go to the next page' (where 'next page' is an URL to sescook2.php).

- All info has to be shown in sescook2.php (Entered name: __, Entered color: ___)

- You have a link 'Go back to previous page' (where 'previous page' is an URL to sescook1.php)

 

My code:

<?php

session_start();

function checkContent() {

if($_POST["name"] == "")

echo "Please enter your name";

elseif($_POST["favcolor"])

echo "Please enter your favorite color";

else

echo "Go to <a href='http://localhost/webadv1011/PHPCookiesSessies/sescook2.php'>next page";

}

?>

 

<form name="color" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" onsubmit="<?php checkContent(); ?>">

<p>Name: <input type="text" name="naam" /></p>

<p>Favourite color: <input type="text" name="favcolor" /></p>

<p><input type="submit" name="submit" value="send" /></p>

</form>

 

This is the code I got so far. My problem is that the page refreshes itself after it does the checkContent function. I'm also not sure how I save my name in a session variable and the color in a cookie variable (and pass them to the next page as well!).

 

We haven't learned anything on cookies and sessions except our own trial and error and things I found on the internet. Unfortunately, I can't really find lots of useful info (most people refer to Ajax, but we can only use php), or I'm looking in the wrong place.

 

Thanks for your help in advance! :)

 

Link to comment
Share on other sites

Try this:

<?php
session_start();
function checkContent() {
   if($_POST["name"] == "")
         echo "Please enter your name";
   elseif($_POST["favcolor"])
         echo "Please enter your favorite color";
   else
      echo "Go to <a href='http://localhost/webadv1011/PHPCookiesSessies/sescook2.php'>next page";
}
?>

<form name="color" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>" onsubmit="<?php checkContent(); ?>">
<p>Name: <input type="text" name="naam" value="<?php echo isset($_POST['naam']) ? $_POST['naam'] : '' ?>" /></p>
<p>Favourite color: <input type="text" name="favcolor" value="<?php echo isset($_POST['favcolor']) ? $_POST['favcolor'] : '' ?>" /></p>
<p><input type="submit" name="submit" value="send" /></p>
</form>

 

Link to comment
Share on other sites

to set your name to a session you can do something like

$_SESSION['name'] = $example_name

to create a cookie refer to http://us3.php.net/manual/en/function.setcookie.php

Also, i advise against using $_SERVER['PHP_SELF']; you can google it to see why..

And I believe you are receiving multiple refreshes etc. because you are trying to incorporate javascript and php in a way that will not give you the results that you want.

Link to comment
Share on other sites

PHP is not like JavaScript.  PHP is run on the server, JavaScript in the browser.  As such, PHP has no notion of a submit event (or any DOM event).  By the time your form is displayed on the screen, your PHP script has finished running.

 

You need something along the lines of:

 

<?php
session_start();

function checkContent()
{
   $errors = array();

   if (empty($_POST['name']))
   {
      $errors['name'] = "Please enter your name";
   }
   else
   {
      $_SESSION['name'] = $_POST['name'];
   }

   if (empty($_POST['favcolor']))
   {
      $errors['favcolor'] = "Please enter your favorite color";
   }
   else
   {
      // store favcolor in a cookie
   }

   if (count($errors) > 0)
   {
      foreach($errors as $error)
      {
         echo $error . '<br />';
      }
   }
   else
   {
      echo "Go to <a href='http://localhost/webadv1011/PHPCookiesSessies/sescook2.php'>next page</a>";
   }
}

if (isset($_POST['submit'))
{
   checkContent();
}
?>

<form name="color" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>Name: <input type="text" name="name" /></p>
<p>Favourite color: <input type="text" name="favcolor" /></p>
<p><input type="submit" name="submit" value="send" /></p>
</form>

 

I left the cookie-setting bit for you to do.

Link to comment
Share on other sites

Thanks for all the help already! :)

 

I didn't really know a lot about the differences between PHP and Javascript, since most of our classes have been self-study...

 

I'll look into the solutions once I get home from class and see if I can solve my problem!

Link to comment
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.