S
S
sugarufc2015-01-09 22:12:31
PHP
sugarufc, 2015-01-09 22:12:31

Sessions in PHP, what's the problem?

I don't know how to elaborate my question, but I'll try.
I want to make it so that in the form input field (input value) the information entered by the user after updating the page is saved and displayed, for this I used the following code

<input type="text" name="username" value="<?=$_SESSION['reg']['username'];?>"/><br /> 
<input type="text" name="name" value="<?=$_SESSION['reg']['name'];?>"/><br />
etc.
Everything is saved and displayed normally, but the problem is that initially when we go to the registration page, an error is displayed in the input field (see photo) 769ce7f7a9ac4fd6a5055f7f031a3483.jpg, and I need it to be empty.
PS I apologize if it is unclear described the problem, but please help those who understand what the point is.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Rishat Kadyrov, 2015-01-09
@sugarufc

Bad but working option:

<input type="text" name="username" value="<[email protected]$_SESSION['reg']['username'];?>"/><br /> 
<input type="text" name="name" value="<[email protected]$_SESSION['reg']['name'];?>"/><br />

I'll just hint at a good one:
$username = '';
if (isset($_SESSION['reg']['username'])) {
    $username = $_SESSION['reg']['username'];
}

I
IceJOKER, 2015-01-09
@IceJOKER

turn off the display of NOTIS in the production version and there will be no problems.
In short - a call to an unknown variable, you need to initialize before you output.
you can check - if there is $_SESSION['reg']['username'] - output it, otherwise empty

X
xmoonlight, 2015-01-09
@xmoonlight

did you try to initialize it on the server before showing the form and fill it with empty lines?)

L
Lorem Ipsum, 2015-01-09
@nobodynoone

Use this
Source function here: stackoverflow.com/questions/9555758/php-default-ar...

function get(&$var, $default=null) {
    return isset($var) ? $var : $default;
}

$test = array('foo'=>'bar');
get($test['foo'],'nope'); // bar
get($test['baz'],'nope'); // nope
get($test['spam']['eggs'],'nope'); // nope
get($undefined,'nope'); // nope

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question