AnimaStone
-
Posts
2 -
Joined
-
Last visited
Never
Posts posted by AnimaStone
-
-
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!
PHP Form unwanted refreshing/redirecting
in PHP Coding Help
Posted
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!