meomike2000 Posted February 2, 2009 Share Posted February 2, 2009 As I am still fairly new at scripting with php, and have never used sessions before, I could use some help. What I have is created a form, and would like to add sessions so that if there is an error in the user filling out the form, when the form reloads and displays the error, they do not have to choose all options again.... here is the options form that i have created, this is just a test form: <html> <head> <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ document.myForm.city.value = ajaxRequest.responseText; } } ajaxRequest.open("GET", "secondchoice.php", true); ajaxRequest.send(null); } //--> </script> <script type="text/javascript" src="js/ajax.js"></script> <script type="text/javascript"> var ajax = new Array(); //this will refresh citys function getCityList(sel) { var stateCode = sel.options[sel.selectedIndex].value; document.getElementById('city').options.length = 0; // Empty city select box if(stateCode.length>0){ var index = ajax.length; ajax[index] = new sack(); ajax[index].requestFile = 'mygetcity.php?stateCode='+stateCode; // Specifying which file to get ajax[index].onCompletion = function(){ createCities(index) }; // Specify function that will be executed after file has been found ajax[index].runAJAX(); // Execute AJAX function } } function createCities(index) { var obj = document.getElementById('city'); eval(ajax[index].response); // Executing the response from Ajax as Javascript code } //this will get subcatagory function getSubCategoryList(sel) { var category = sel.options[sel.selectedIndex].value; document.getElementById('subcat').options.length = 0; // Empty subcat select box if(category.length>0){ var index = ajax.length; ajax[index] = new sack(); ajax[index].requestFile = 'mygetsubcat.php?category='+category; // Specifying which file to get ajax[index].onCompletion = function(){ createSubCategories(index) }; // Specify function that will be executed after file has been found ajax[index].runAJAX(); // Execute AJAX function } } function createSubCategories(index) { var obj = document.getElementById('subcat'); eval(ajax[index].response); // Executing the response from Ajax as Javascript code } </script> <title>test</title> <link rel="stylesheet" type="text/css" href="../web1.css" /> <link rel="stylesheet" type="text/css" href="../heading.css" /> <link rel="stylesheet" type="text/css" href="../navigation.css" /> <link rel="stylesheet" type="text/css" href="directory.css" /> </head> <body> <div class="navigation" id="navigation"> <a class="navjust" href="../index.php">Home</a><br /> <br /> <a class="navjust" href="http://mail.meo2000.net">Check Mail</a><br /> <br /> <a class="navjust" href="../web/webservice.php">Web Services</a><br /> </div> <div id="justify"> <div id="h"> <h1>meo2000 listing service</h1> <h3>want to get listed, click <a href="getlisted.htm">here</a> to find out how.</h3> <h3>This directory is under construction!</h3> <br/> </div> </div> <?php Function DisplayForm($errors = null){ $error_string = $errors; //state, you could use $error_string = implode ("<br />",$errors); if you want to save performance by using an array insead. echo(' <div id="left"> <span class="error"> '.$error_string.'<br /> </span> <form method="post" action="'.$_SERVER['PHP_SELF'].'"> State: <select id="state" name="state" onchange="getCityList(this)"> <option value="">Select a state</option> <option value="Alabama">Alabama</option> <option value="North Carolina">North Carolina</option> <option value="Texas">Texas</option> </select><br /> <br /> City: <select id="city" name="city"> </select><br /> <br /> Category: <select id="category" name="category" onchange="getSubCategoryList(this)"> <option value="">Select a catagory</option> <option value="Auto">Auto</option> <option value="Hotel">Hotel</option> <option value="Restaurant">Restaurant</option> </select><br /> <br /> SubCategory: <select id="subcat" name="subcat"> </select><br /> <br /> <input type="submit" name="submit" value="Select"> </form> </div> '); } //check if form has been submitted if (!$_POST['submit']) { //if not display form DisplayForm(); } else { //form has been submitted //items selected, check that number was entered, //Declare it so we dont get notices 'undeclared variable' $error = null; //check state has been selected if (trim($_POST['state']) == "") { $error .='ERROR: Please select a state.'; } //check category has been selected elseif (trim($_POST['category']) == "") { $error .='ERROR: Please select a category.'; } //if there was an error, show it. if ($error != null){ DisplayForm($error); } else { //otherwise carry on? echo '<div id="right">'; $state = $_POST['state']; $city = $_POST['city']; $cat = $_POST['category']; $subcat = $_POST['subcat']; echo 'Here is your selections: <br />'; echo "<i>$state</i><br />"; echo "<i>$city</i><br />"; echo "<i>$cat</i><br />"; echo "<i>$subcat</i><br />"; echo '</div>'; } } ?> <div class="addspace" id="addspace"> <p class="none" id="addmargin"> your<br > add<br > could<br > be<br > filling<br > this<br > spot! </p> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
btherl Posted February 2, 2009 Share Posted February 2, 2009 Try making some simple scripts with sessions first, to get an idea of how they work. Going straight into a bigger task like this gives you more mistakes to make, making it harder to learn. Eg, start with a script which remembers the contents of a single text field and puts them back in every time. Then use the same principle for the larger script. Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 3, 2009 Author Share Posted February 3, 2009 as i have no clue how to use sessions, could somebody give me and example using this simple form please. I have read many how-to-s on the topic but the ones i have found give little info on how to actually use them. i need an example to get me started. <html> <head></head> <body> <?php //check if form has been submitted if (!$_POST['submit']) { //if not display form ?> <form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> please enter your name: <input type="text" name="name" size="15"><br > <br > <input type="submit" name="submit" value="Select"> </form><br> <?php } else { $name = $_POST['name']; echo "$name <br >"; } ?> </body> </html> thanks in advance, mike..... Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted February 3, 2009 Share Posted February 3, 2009 I will give you a pretty good example that you can build off of. This is how you start a session and set a value: <?php session_start(); $_SESSION['username'] = "myusername"; ?> That will set the session value of username equal to myusername. Now on another page you can call that by doing this: <?php session_start(); echo $_SESSION['username']; ?> When you are ready to stop the session (logout of a website) you can do this: <?php session_start(); session_destroy(); ?> The session destroy kills the current session. So basically the session_start() needs to be called on any page where the session is to be carried. When you want to end the session you can call the session_destroy. You will more than likely want to set the session variables on login. When you authenticate the user you can set the session variables. You set session variables like you would a regular variable. Any questions feel free to ask. Check out this tutorial it is pretty good http://www.tizag.com/phpT/phpsessions.php Quote Link to comment Share on other sites More sharing options...
btherl Posted February 3, 2009 Share Posted February 3, 2009 That's a good example, thanks I'm too lazy to write one .. Here's how I view sessions - As long as you call session_start(), then everything you put in $_SESSION will still be there next time your script runs. This won't be true if the user deliberately clears the session on his end, or if they wait too long and the session is garbage collected by php, but that's the general idea of how they work. You just treat $_SESSION as a spot to store things that you want to persist. Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 3, 2009 Author Share Posted February 3, 2009 that is good, and i have seen the how-to that you linked at tigzag.com. i must be missing something, or i just dont get it. i have a form like the one below, this form uses validation to make sure that a user has filled in all the input fields. if not the form reloads and i would like for the user to not have to fill in the entire form again. could somebody show me how to add sessions to make this happen. here is the sample form. <html> <head> </head> <body> <?php Function DisplayForm($errors = null){ $error_string = $errors; echo(' '.$error_string.'<br /> please enter your name: <input type="text" name="name" size="15"><br > <br > Please enter a number between 1 and 10: <input type="text" name="num" size="2"> <br /> <input type="submit" name="submit" value="Select"> </form> '); } //check if form has been submitted if (!$_POST['submit']) { //if not display form DisplayForm(); } else { //form has been submitted //items selected, check that number was entered, $error = null; //Declare it so we dont get notices 'undeclared variable' //check number range if (!isset($_POST['num']) || !is_numeric($_POST['num'])) { $error .='ERROR: Please enter a number between 1 and 10'; } elseif ($_POST['num'] < 1 || $_POST['num'] > 10) { $error .='ERROR: The number must be between 1 and 10'; } //if there was an error, show it. if ($error != null){ DisplayForm($error); } else { //otherwise carry on? $name = $_POST['name']; $num = $_POST['num']; echo 'Here is your selections: <br />'; echo "<i>$name</i><br />"; echo "<i>$num</i><br />"; } } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
ngreenwood6 Posted February 3, 2009 Share Posted February 3, 2009 From what I showed you, you should be able to make an attempt. Try something, then when you get stuck come back here and I will help you further. If I just do it for you how will you learn. Trial and error will only better your skills by learning to troubleshoot when you run into problems. Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 3, 2009 Author Share Posted February 3, 2009 i think that you might misunderstand what i am trying to say. i get the idea behind sessions. i understand how they work. in this example: <?php session_start(); $_SESSION['views'] = 1; // store session data echo "Pageviews = ". $_SESSION['views']; //retrieve data ?> i understand how it assigns a value of 1 to views, and the echo command displays the count. i also understand that in this example: <?php session_start(); if(isset($_SESSION['views'])) $_SESSION['views'] = $_SESSION['views']+ 1; else $_SESSION['views'] = 1; echo "views = ". $_SESSION['views']; ?> we first check if views has already been set and if so we add 1 to it, if not then views equal 1. then we once again display the value of views. i get the basics. i am not asking for you to give me the answer. but all of these examples we directly assign a value, and then echo it back. what i do not get is does all this have to be at the top of the page or can it be mixed into my form or what.... that is what i dont get....... thanks mike....... Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 3, 2009 Author Share Posted February 3, 2009 ok i created this simple form,: <?php session_start(); if(isset($_SESSION['name'])) { $_POST['name'] = $_SESSION['name']; } else { $_SESSION['name'] = null; } echo $_SESSION['name']; ?> <html> <head> <link rel="stylesheet" type="text/css" href="web1.css" > <link rel="stylesheet" type="text/css" href="heading.css" > <link rel="stylesheet" type="text/css" href="navigation.css" > </head> <body> <div class="navigation" id="navigation"> <a class="navjust" href="index.php">home</a><br > <br > </div> <?php //check if form has been submitted if (!$_POST['submit']) { //if not display form ?> <div id="justify"> <form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> please enter your name: <input type="text" name="name" size="15"><br > <br > <input type="submit" name="submit" value="Select"> </form></div><br> <?php } else { echo '<div id="justify">'; echo '<p class="top">'; $name = $_POST['name']; echo "$name <br >"; echo '</p>'; echo '</div>'; } ?> </body> </html> and after entering a name in the input box and submitting it displays the name, then i can click on the home link and go back to my home page. then if i click on the link to go back to this same form, it will display the name i put in the box the first time at the top of the page. but i want it to put it back in the box again. how would i do that. also now everytime i visit the page no matter what i type in the box it still displays what i put in the first time, even after i submit it....... Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 3, 2009 Author Share Posted February 3, 2009 i also get this error the first time i click submit: Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0 i am using php5 Quote Link to comment Share on other sites More sharing options...
meomike2000 Posted February 3, 2009 Author Share Posted February 3, 2009 ok i added this and it seams to work. till get that global error though: [b]<?php session_start(); if(isset($_SESSION['name'])) { echo $_SESSION['name']; } else { $_SESSION['name'] = null; } ?> [/b] <html> <head> <link rel="stylesheet" type="text/css" href="web1.css" > <link rel="stylesheet" type="text/css" href="heading.css" > <link rel="stylesheet" type="text/css" href="navigation.css" > </head> <body> <div class="navigation" id="navigation"> <a class="navjust" href="index.php">home</a><br > <br > </div> <?php //check if form has been submitted if (!$_POST['submit']) { //if not display form ?> <div id="justify"> <form method="POST" action="<?=$_SERVER['PHP_SELF']?>"> please enter your name: <input type="text" name="name" size="15"[b] value="<?=$_SESSION['name']?>[/b]"><br > <br > <input type="submit" name="submit" value="Select"> </form></div><br> <?php } else { [b]$_SESSION['name'] = $_POST['name'];[/b] echo '<div id="justify">'; echo '<p class="top">'; $name = $_POST['name']; echo "$name <br >"; echo '</p>'; echo '</div>'; } ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.