Jump to content

using "SESSION"


siabanie

Recommended Posts

Hi all,

 

I would like to know how we can retrieve all the values / data that we filled in the form so when we press the button "back" it is not disappear?

 

I have three pages which are, checkForm.php, processForm.php and thankyouForm.php

 

Where I should put the SESSION code?

 

Here is the snippets of my checkForm.php:

..
..
<table width="100%" border="0" cellspacing="5" cellpadding="0">
  <tr>
    <td width="20%">Name</td>
    <td width="80%"><input name="name" type="text" id="name" size="30" /></td>
  </tr>
  <tr>
    <td>Tel</td>
    <td><input name="tel" type="text" id="tel" size="30" /></td>
  </tr>
  <tr>
    <td>E-mail</td>
    <td><input name="email" type="text" id="email" size="30" /></td>
  </tr>
</table>
....

 

Thanks.

Link to comment
Share on other sites

You shouldn't use a session for this. Assuming the form isn't shown on the page with the back button, just include all the form data as hidden inputs. When the data is then submitted back to the form, you just need something like this on each input:

 

<input type="text" name="example" value="<?php if (isset($_POST['example'])) echo htmlspecialchars($_POST['example']); ?>" />

Link to comment
Share on other sites

Hi Adam,

 

Thanks for your feedback, I have tried as you suggested as:

 

..
..
<tr>
    <td width="20%">Name</td>
    <td width="80%"><input name="name" value="<?php if (isset($_POST['name'])) echo htmlspecialchars($_POST['name']); ?>" type="text" id="name" size="30" /></td>
  </tr>
  <tr>
..
..

 

I filled the form etc then submit it, when I press back button or click the back link (I use Javascript script to go back e.g: Please go <a href='javascript:history.go(-1)'>back</a> and try again) - The field name is empty?

 

Am I missing something here? -- And yes I have put the

 session_start();

at the top of every page. Any idea what I did wrong?

Link to comment
Share on other sites

you should use javascript to validate your form then ajax submit.

 

Hi ZulfadlyAshBurn, I am using JavaScript to validate the form e;g:

 

$(document).ready(function(){

  $('#btn_submit').click(function() {
    var error_num = 0;
    var error_mesg = "";
    //alert("in");
    
    $('[name=name]').parent().removeClass('error');
    if($('[name=name]').val()==""){
      error_num++;
      error_mesg += "Please Enter Your Name。\n";
      $('[name=name]').parent().addClass('error');
    }

$('[name=captcha_code]').parent().removeClass('error');
    if($('[name=captcha_code]').val()==""){
      error_num++;
      error_mesg += "Please enter the image verification\n";
      $('[name=captcha_code]').parent().addClass('error');
    }
    
    if(error_num>0){
      alert(error_mesg);
    }else if(error_num==0){
      $('#present_form').submit();
    }
    return false;
    
  });

});
</script>

 

But the problem is; whenever I press "back" button all the values that I have just filled in gone then I have to fill it again which is annoying -- Plus How I can validate the securimage CAPTCHA (http://www.phpcaptcha.org/) using AJAX?

 

I cannot seem able to show the error message on the same page but able to do it for the next page (If user clicked submit form and entered a wrong code)

Link to comment
Share on other sites

[...] include all the form data as hidden inputs. When the data is then submitted back [...]

 

Let's say the form is in form.php, and you process it in a file called process.php. When the data is submitted to process.php from form.php, you should include all the form data you need to pass back, within another form in process.php but as hidden inputs. The user won't know see the inputs, but it's effectively like them filling it all in again.

 

Clicking back should submit said form, effectively posting the data back to form.php, but from process.php. Then the code I provided before will work.

Link to comment
Share on other sites

i see the problem (and probably confusion). The back button your referring to is the browser back button (or an equivalent hijacking of its function via javascript anchor). What your trying to accomplish is difficult, simply because you have to contend with browser caching and everything else. Plus you cant post back data.

 

You have 2 options.

 

1. use the session. Its generally frowned upon, but sometimes it's the only way

2. create your own back button link that does as adam suggested basically

<form>
<div  style='display:hidden'>
<!-- put all your inputs here with their names the same as the real form and the value attribute set to $_POST[input_name]-->
</div>
<input type='submit' value='Go Back' />
</form>

That would basically cause you to go back to the form page, but it will post back all the form data so you can prepopulate the form again.

 

 

However javascript validation should cover most error's anyway. So if you implement ood JS validation, the odd user getting caught out via the backend script shouldn't be an issue

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.