Jump to content

Prevent code from running when page loads


Ramtree

Recommended Posts

i have a php page where user fills up the forms.

 

this is the code i use:

if(empty($_POST['form']))
{
echo "please fill in the forms";
}
else
{
mysql_query($sql);
}

 

when the page loads, it would display "please fill in the forms". i want to prevent the php code from running when the user first go to the page. please tell me how to do so, thanks.

$error=0;


if(!empty($_POST) && empty($_POST['form1']))
{
echo "Please input the Question.";
echo "<br>";
$error++;
}

if(!empty($_POST) && empty($_POST['form2']))
{
echo "Please input the Answer Option 1.";
echo "<br>";
$error++;
}

if(!empty($_POST) && empty($_POST['form3']))
{
echo "Please input the Answer Option 2.";
echo "<br>";
$error++;
}

if(!empty($_POST) && empty($_POST['form4']))
{
echo "Please input the Answer Option 3.";
echo "<br>";
$error++;
}

if(!empty($_POST) && empty($_POST['form5']))
{
echo "Please input the Answer Option 4.";
echo "<br>";
$error++;
}

if(!empty($_POST) && empty($_POST['form6']))
{
echo "Please input the Correct Answer.";
echo "<br>";
$error++;
}


if($error==0)
{
echo "Request submitted successfully.";
mysql_query($sql);
}

 

 

if were to use the code like this, the page would display "Request submitted successfully." and blank data would be inserted in to the database. i want to prevent this code from running as well. pls help me, thanks. :)

if (!empty($_POST)) {
    if (empty($_POST['field'])) {
        $errors[] = 'field=empty';
    } else {
        //field is not empty
    }
    ..
    if (sizeof($errors)) {
        print 'errors';
    } else {
        //process
    }
}

Archived

This topic is now archived and is closed to further replies.

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