12 Replies - 2198 Views - Last Post: 18 May 2011 - 12:09 PM Rate Topic: -----

#1 sidhu_sree  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 37
  • Joined: 08-October 09

php session variable returns array

Posted 17 May 2011 - 10:35 PM

hello there

i am using session to store user name in my site and display user name using them
but in one script after submitting the form
and executing the result it redirected to the same form
now i got problem here
user session name is showing array instead of user name

suggest plz thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: php session variable returns array

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: php session variable returns array

Posted 18 May 2011 - 12:04 AM

We could help you better if you show us the source code (In CODE tags).
Was This Post Helpful? 0
  • +
  • -

#3 sidhu_sree  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 37
  • Joined: 08-October 09

Re: php session variable returns array

Posted 18 May 2011 - 12:39 AM

hello hi

i am doing a project here in user registration
am implementing server side validations only
after submitting thew form the if error are found the page is redirected to form
but the form date is no more here

how can i restore the previous form data in the form

i have some idea to implement this using session variables but i feel it is not a good practice
so any one can suggest me a good method to restore previous form data

thanks in advance
Was This Post Helpful? 0
  • +
  • -

#4 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: php session variable returns array

Posted 18 May 2011 - 03:13 AM

I don't think redirection on failed validation of user input is necessary (there by leading to the unnecessary use of session variables) only when validation test passes. This is how you should go about it:

Quote

Perform validation on user input on registration page submission to the server.
if validation fails
populate an array of validation error messages for every failed validation (like
"Username already exist") and display it on the form.
else if validation succeeds
then you can redirect to the necessary page of choice.


To the pressing issue at hand, since i'm from a .NET background, we enable a property "View state" in ASP.NET to achieve this effect. But since I moved over to PHP(not too long ago), I wrote a simple function to emulate that behaviour. That way when the server returns its response (in the event of failed user input validation) the initial input by the user remains. Here is the method (using a ternary operator) and what follows is how I use it on my registration form pages:

 function displayViewState($formElementValue)
{
  return (empty($formElementValue)) ? "" : $formElementValue;
}



And on the registration form, I use it like so:
<input name="lastname" id="lastname" type="text" value="<?php if(isset($_POST['submit'])){echo displayViewState($_POST['lastname']);}?>"/>
		  <span id="errorContainer1" class="errorContainer">
		  <?php
                    if(!empty($errMsg['lastname']))		  
		       echo $errMsg['lastname'];
		    else
		       echo "";
		  ?>
		  </span>



Notice the error message array I talked about- $errMsg['element name']? You only redirect to another page if validation passes else you display the validation errors on the registration form page. That's how I do stuff like that. Hope this helps.
Cheers

This post has been edited by Ace26: 18 May 2011 - 03:26 AM

Was This Post Helpful? 1
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: php session variable returns array

Posted 18 May 2011 - 03:49 AM

Ace26, I am not a PHP expert, so correct me if I'm wrong. When 'submit' is clicked, without using javascript, you have to redirect to a new page (or stay on same page, but still the page is refreshed). if no errors are found, what are you doing in that case? how do you redirect the user?
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: php session variable returns array

Posted 18 May 2011 - 04:12 AM

I don't know about ASP but in PHP the page MUST be refreshed to be executed. As stated above you can store the variables in the session and simply echo them back out to input values.
Was This Post Helpful? 2
  • +
  • -

#7 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: php session variable returns array

Posted 18 May 2011 - 04:16 AM

View Postjapanir, on 18 May 2011 - 04:49 AM, said:

Ace26, I am not a PHP expert, so correct me if I'm wrong. When 'submit' is clicked, without using javascript, you have to redirect to a new page (or stay on same page, but still the page is refreshed). if no errors are found, what are you doing in that case? how do you redirect the user?


When submit is clicked, the page is submitted to the server for processing(or action) by the script contained in the form element's action attribute.

Ideally when its processing is done(based on the script), the server should return a response to the requesting page. In this case however, the interpreter processing according to the script written, would validate the user input and if all is valid redirect to another page else returns a response to the requesting page.

You cool with that?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: php session variable returns array

Posted 18 May 2011 - 04:19 AM

Merged duplicate topics. Please do NOT create multiple topics on the same subject/problem.
Was This Post Helpful? 0
  • +
  • -

#9 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: php session variable returns array

Posted 18 May 2011 - 04:52 AM

For codeprada and Japanir,

<input name="lastname" id="lastname" type="text" value="<?php if(isset($_POST['submit'])){echo displayViewState($_POST['lastname']);}?>"/>
          <span id="errorContainer1" class="errorContainer">
          <?php
            if(!empty($errMsg['lastname']))      
               echo $errMsg['lastname'];
            else
               echo "";
          ?>
          </span>


I'll explain what happens above and why I said using session variable was unnecessary.

When the page is posted back to the server, the interpreter interprets every thing contained within the
<?php?>
tag. Hence when the server is sending back it's response to the browser to display(if not a new page based on redirection), the value of the textbox lastname is set to $_POST['lastname'] thereby retaining the user's initial input.

Hope this makes it clearer why you shouldn't bother busy ol' session variables for retaining user input after "refreshing" on return from the server.

cheers

This post has been edited by Ace26: 18 May 2011 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: php session variable returns array

Posted 18 May 2011 - 05:40 AM

@Ace26 I never said you did.

Quote

i have some idea to implement this using session variables but i feel it is not a good practice
so any one can suggest me a good method to restore previous form data


@sidhu_sree If you could post just that section of code so we can advice you of what not to and to do.
Was This Post Helpful? 0
  • +
  • -

#11 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: php session variable returns array

Posted 18 May 2011 - 09:24 AM

View PostAce26, on 18 May 2011 - 06:52 AM, said:

Hope this makes it clearer why you shouldn't bother busy ol' session variables for retaining user input after "refreshing" on return from the server.cheers


Personally I would hate to see your form and have to go through and edit it for one reason or another.

example with only 2 inputs, this was after I fixed your indentation and use of curly brackets on your conditional statements, which is not all necessary but makes the code EASIER to read.
<form action="action.php" method="post">
<input name="lastname" id="lastname" type="text" value="<?php if(isset($_POST['submit'])){ echo displayViewState($_POST['lastname']); }?>"/>
	<span id="errorContainer1" class="errorContainer">
		<?php
			if(!empty($errMsg['lastname'])) {  
				echo $errMsg['lastname'];
			} else {
				echo "";
			}
		?>
	</span>
<input name="lastname" id="lastname" type="text" value="<?php if(isset($_POST['submit'])){ echo displayViewState($_POST['lastname']); }?>"/>
	<span id="errorContainer1" class="errorContainer">
		<?php
			if(!empty($errMsg['lastname'])) {  
				echo $errMsg['lastname'];
			} else {
				echo "";
			}
		?>
	</span>
</form>



it's cluttered and has no flow in my opinion, true you don't HAVE to use the $_SESSION array to carry values but you can very much clean up your code doing so, i have a simple class I use on simple forms for something like this and when I'm not using a templating engine (which EVERYONE should look into to split display & processing code, again IMO), I use this simple class which utilizes sessions to create data arrays and error arrays making the form cleaner to look at, not to mention now you have a OOP back end which can be leaps and bounds more flexible (IMO).

<form action="action.php" method="post">
	<label for="firstname">First Name:</label>
	<input type="text" name="firstname" value="<?php $form->getData('firstname'); ?>" />
	<font color="red"><?php $form->getError('firstname'); ?></font>
	
	<br/>
	
	<label for="lastname">Last Name:</label>
	<input type="text" name="lastname" value="<?php $form->getData('lastname'); ?>" />
	<font color="red"><?php $form->getError('lastname'); ?></font>
	
	<br/>
	<input type="submit" value="Submit"/>
</form>



I'm not one to favor going in and out of PHP which HTML or vice versa just a pointer separating your code could make this even simpler and easier to read.

Just my 2 cents on the matter.
Was This Post Helpful? 1
  • +
  • -

#12 Ace26  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 183
  • Joined: 10-August 08

Re: php session variable returns array

Posted 18 May 2011 - 11:45 AM

Did it for a second occur to you there that the way the code displays on these forums is most times not the way it really is in your source files? I always try as much possible to indent my code properly. As for the conditionals that have only one statement after the condition statement, like you said, the curly braces aren't necessary as far as you indent properly even though I include them sometimes.

View PostRPGonzo, on 18 May 2011 - 10:24 AM, said:

Personally I would hate to see your form and have to go through and edit it for one reason or another.


Without having actually not seen the way I write code, I think that statement is a bit too forward and harsh, friend. But thanks for your critique, anyways.

Well, let's just put all that bitching aside and concentrate on helping the fellow now, shall we?

This post has been edited by Ace26: 18 May 2011 - 11:48 AM

Was This Post Helpful? 0
  • +
  • -

#13 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: php session variable returns array

Posted 18 May 2011 - 12:09 PM

Without cluttering this thread I will state why I said what I said and stand by it.

This was not to be a personal attack on yourself nor your coding style.

In my opinion (and its just that MY opinion) the way you help someone reflects the way you care for your own projects, if you show someone how to do something with an example that has poor attributes it doesn't say much for your intentions on your own projects.

With you this very well may not be the case, but I wanted to put my input in on the subject while the subject was at hand is all.

Now if the OP would actually post some code for others to be able to help them, maybe this thread would be some help to the OP and others coming across it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1