-1

I have a form that is being validated using PHP server side scripting. To show multiple errors, I store it in an array and used foreach to loop through the values and echo it for dislay. I thought i'm finished using PHP validation but I noticed that everytime I submit the form, even though it still contains some errors, all of the values that were inputted resets. Why? When I used javascript, the values remains so whats with PHP? The form's action is also to itself.

You could take a look at this. Disable your javascript to see what I'm pointing out.
http://angelickp.byethost24.com/AliceEnrollment.php

Edited by AngelicOne: n/a

3
Contributors
14
Replies
15
Views
6 Years
Discussion Span
Last Post by urtrivedi
Featured Replies
  • 2
    diafol 3,720   6 Years Ago

    Data is wiped as soon as you send, unless you do back button. You send the form to the server, so essentially you're reloading the page. Javascript just checks data using your browser - do not rely on js for validation. Also avoid sending the data to itself. This ensures … Read More

  • in your post handler form you check errors [CODE] <?php unset($_SESSION['last_data']); unset($_SESSION['errors']); if(errors) { //set error session here $_SESSION['last_data']=$_POST; } else { //update/insert database } header("location:myform.php"); exit; ?> [/CODE] Now in your main form you can diplay session errors if session errors is set, and use last data to set … Read More

  • [CODE] <input type=checkbox name=chk1 id=chk1 value='Yes' <?php if ($_SESSION['last_data']['chk1']=='Yes') echo "checked"; ?> > Check here <input type=radiobutton name=rad1 id=rad1 value='No' <?php if ($_SESSION['last_data']['rad1']=='No') echo "checked"; ?> > No <select name=sel id=sel > <option value='v1' <?php echo ($_SESSION['last_data']['sel']=='v1'?"selected":"") ?> > One</option> <option value='v2' <?php echo ($_SESSION['last_data']['sel']=='v2'?"selected":"") ?> > Two</option> <option value='v3' … Read More

  • 1
    diafol 3,720   6 Years Ago

    [ICODE]$_SESSION["type"][/ICODE] will be an array. So you shouldn't check that way, you could do: [CODE]if(in_array("A",$_SESSION['type'])){...}[/CODE] Just ensure that the session var is an array - force it with (array). Read More

2

Data is wiped as soon as you send, unless you do back button. You send the form to the server, so essentially you're reloading the page. Javascript just checks data using your browser - do not rely on js for validation.

Also avoid sending the data to itself. This ensures that data is sent AGAIN on page reload/refresh - not good. Send it to a data handler, this sends error messages and return data back to the form or another page.

AN easy way to repopulate your form:

data handler:

$_SESSION['last_data'] = $_POST;

if data errors - redirefct back to form:

if(isset($_SESSION['errors'])){
...do messages into local vars...
...do data into local vars...
unset($_SESSION['errors']);
unset($_SESSION['last_data']);
}

Edited by diafol: n/a

Votes + Comments
0

Data is wiped as soon as you send, unless you do back button. You send the form to the server, so essentially you're reloading the page. Javascript just checks data using your browser - do not rely on js for validation.

Also avoid sending the data to itself. This ensures that data is sent AGAIN on page reload/refresh - not good. Send it to a data handler, this sends error messages and return data back to the form or another page.

AN easy way to repopulate your form:

data handler:

$_SESSION['last_data'] = $_POST;

if data errors - redirefct back to form:

if(isset($_SESSION['errors'])){
...do messages into local vars...
...do data into local vars...
unset($_SESSION['errors']);
unset($_SESSION['last_data']);
}

Where's exactly the line wherein it redirects back to form.

1

in your post handler form you check errors

<?php
unset($_SESSION['last_data']);
unset($_SESSION['errors']);
if(errors)
{
//set error session here
$_SESSION['last_data']=$_POST;
}
else
{
//update/insert database
}

header("location:myform.php");
exit;
?>

Now in your main form
you can diplay session errors if session errors is set, and use last data to set back values

<?php
if(isset($_SESSIONS['errors'])
{
//display errorrs at the top of page
}

.
.
.
.
<input type=text name=txt1 id=txt1 value='?php echo  $_SESSION['last_data']['txt1']?>' >
.
.
.
.
?>

Edited by urtrivedi: n/a

Votes + Comments
Redirecting a form is something new to me using PHP! Thank you very much!
0

in your post handler form you check errors

<?php
unset($_SESSION['last_data']);
unset($_SESSION['errors']);
if(errors)
{
//set error session here
$_SESSION['last_data']=$_POST;
}
else
{
//update/insert database
}

header("location:myform.php");
exit;
?>

Now in your main form
you can diplay session errors if session errors is set, and use last data to set back values

<?php
if(isset($_SESSIONS['errors'])
{
//display errorrs at the top of page
}

.
.
.
.
<input type=text name=txt1 id=txt1 value='?php echo  $_SESSION['last_data']['txt1']?>' >
.
.
.
.
?>

I'm new to session so pardon me if I have some simple questions to ask.

I believe that this code

<?php
unset($_SESSION['last_data']);
unset($_SESSION['errors']);
if(errors)
{
//set error session here
$_SESSION['last_data']=$_POST;
}
else
{
//update/insert database
}

header("location:myform.php");
exit;
?>

is from another php page. This page will be the value of the main form's action attribute. The 1st line of codes is deleting the sessions. Does that mean that before going to that code, I should start session and set session variables at the main form? Also, you started it with unset, it means remove the sessions right? So how can you use the variables if it was unset at the very first line of the script.

What are the values of $_SESSION? I'm not quite getting it.

This is how I set my errors and insert to data to db without sessions.

<?php

if(isset($_POST["submit"]))
{
$lname = $_POST["lname"];

$errors = array();

if(strlen($lname) == 0)
{
$errors[] = "Last name is required";
}

if (!empty(errors))
{
//display errors using foreach loop
}
else
{
//connect and insert data to mysql db
}

}
?>

Edited by AngelicOne: n/a

0

To be exact. What does this line tell?

if(errors)

If it is set or something? What exactly is it? What values does it have?

0

After reading tutorials about sessions and cookies, I managed to do this! I finally understand what you're pointing out. Thank you very much. I have one last question, how to do this with radiobutton, dropdowns and checkboxes?

1
<input type=checkbox name=chk1 id=chk1 value='Yes' 
<?php 
   if ($_SESSION['last_data']['chk1']=='Yes')
    echo "checked";
?>   > Check here


<input type=radiobutton name=rad1 id=rad1 value='No' 
<?php 
   if ($_SESSION['last_data']['rad1']=='No')
    echo "checked";
?>   > No


<select name=sel id=sel >
<option value='v1' <?php echo ($_SESSION['last_data']['sel']=='v1'?"selected":"") ?>     > One</option>

<option value='v2' <?php echo ($_SESSION['last_data']['sel']=='v2'?"selected":"") ?>     > Two</option>


<option value='v3' <?php echo ($_SESSION['last_data']['sel']=='v3'?"selected":"") ?>     > Three</option>

</select>

Edited by urtrivedi: n/a

0
<input type=checkbox name=chk1 id=chk1 value='Yes' 
<?php 
   if ($_SESSION['last_data']['chk1']=='Yes')
    echo "checked";
?>   > Check here


<input type=radiobutton name=rad1 id=rad1 value='No' 
<?php 
   if ($_SESSION['last_data']['rad1']=='No')
    echo "checked";
?>   > No


<select name=sel id=sel >
<option value='v1' <?php echo ($_SESSION['last_data']['sel']=='v1'?"selected":"") ?>     > One</option>

<option value='v2' <?php echo ($_SESSION['last_data']['sel']=='v2'?"selected":"") ?>     > Two</option>


<option value='v3' <?php echo ($_SESSION['last_data']['sel']=='v3'?"selected":"") ?>     > Three</option>

</select>

My checkbox and dropdowns is quite large. Is this the only way? Checking it one by one?

0

I've done this.

data handler

foreach($type as $types) // to store multiple values from checkbox
{
$_SESSION["type"] = $types;
}

main form

<input type="checkbox" name="type[]" id="A" value="A" <?php if(isset($_SESSION["errors"])) { if($_SESSION["type"] == "A") {  
echo "CHECKED"; } }?>/>

But doing this with every items makes only one check at a time even there are more than two items checked.

Edited by AngelicOne: n/a

1

$_SESSION["type"] will be an array. So you shouldn't check that way, you could do:

if(in_array("A",$_SESSION['type'])){...}

Just ensure that the session var is an array - force it with (array).

Votes + Comments
in_array checks a value in an array. I learned someting new. Thanks!
0

$_SESSION["type"] will be an array. So you shouldn't check that way, you could do:

if(in_array("A",$_SESSION['type'])){...}

Just ensure that the session var is an array - force it with (array).

Everytime the script execute, the element dissapears and the ones that comes after it.

I believe that setting a session variable to an array is the same as setting a regular variable. So in my data handler I use

$_SESSION["type"] = $type;

The variable type is from the names of my checkbox group.

//html
<input type="checkbox" name="type[]" value="A"/>

//php
$type = $_POST["type"]

What's wrong with this?

<input type="checkbox" name="type[]" value="A" <?php if(isset($_SESSION["errors"])) { if (inarray("A",$_SESSION["type"])) { echo "CHECKED"; }} ?> />

Edited by AngelicOne: n/a

0

I have a question about dropdown. I have this dropdown that contains a list of countries, about 50 I think. Do I really need to put the conditional statement for each item?

0

If you are writing all options on your own (hard coding) then you have to write all conditions,
but if you have table in mysql, then you can use for loop.

Edited by urtrivedi: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.