php session variable returns array
Page 1 of 112 Replies - 1541 Views - Last Post: 18 May 2011 - 12:09 PM
#1
php session variable returns array
Posted 17 May 2011 - 10:35 PM
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
Replies To: php session variable returns array
#2
Re: php session variable returns array
Posted 18 May 2011 - 12:04 AM
#3
Re: php session variable returns array
Posted 18 May 2011 - 12:39 AM
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
#4
Re: php session variable returns array
Posted 18 May 2011 - 03:13 AM
Quote
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
#5
Re: php session variable returns array
Posted 18 May 2011 - 03:49 AM
#6
Re: php session variable returns array
Posted 18 May 2011 - 04:12 AM
#7
Re: php session variable returns array
Posted 18 May 2011 - 04:16 AM
japanir, on 18 May 2011 - 04:49 AM, said:
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?
#8
Re: php session variable returns array
Posted 18 May 2011 - 04:19 AM
#9
Re: php session variable returns array
Posted 18 May 2011 - 04:52 AM
<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
#10
Re: php session variable returns array
Posted 18 May 2011 - 05:40 AM
Quote
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.
#11
Re: php session variable returns array
Posted 18 May 2011 - 09:24 AM
Ace26, on 18 May 2011 - 06:52 AM, said:
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.
#12
Re: php session variable returns array
Posted 18 May 2011 - 11:45 AM
RPGonzo, on 18 May 2011 - 10:24 AM, said:
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
#13
Re: php session variable returns array
Posted 18 May 2011 - 12:09 PM
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.
|
|

New Topic/Question
Reply




MultiQuote





|