Jump to content

Required fields on a submit


turbocueca

Recommended Posts

Post your error messages here along with your code then we'll be able to help you a lot more. Basically fi you want to see if certain field is emtpy the use this:
[code]if(!isset($_POST['FieldName']) || empty($_POST['fileName']))
{
    echo "PLease fill in all fields thank you!;
}[/code]
Link to comment
Share on other sites

a quick way i found to check in general is

foreach($_POST[] as $post) {
if ($post == '') {
$error("Blank field detected");
}
}

Then you just do
if (!$error) {
all code to insert/upload the data
} else {
normal form to display and echo all the variables abck into the boxes to save the user som hastle
}

As far as i've tested on my own pages that works :)
Link to comment
Share on other sites

the var $post is the one that has the value of the field right?

And what does the ! behind $error or isset as I sometimes see?

it gives this error:


Warning: Invalid argument supplied for foreach() in /home/www/infocenter.awardspace.com/inter/ps2ub/insert.php on line 21
Link to comment
Share on other sites

[code]   <?php    if (isset($_POST['Submit'])) {
           
        include 'db.php';

        $n=$_POST['textfield3'];
        $s=$_POST['radiobutton1'];
        $w=$_POST['radiobutton'];        
        $c=$_POST['textfield'];
        $p=$_POST['textfield2'];
        $e=$_POST['textfield22'];
        
        foreach($_POST[] as $post) {
        if ($post == '') {
        $error("Blank field detected");
        }
        }
        
        if (!$error) {
        $query="INSERT INTO usbextreme (name,system,work_status,comments,postedby,email) VALUES     ('$n','$s','$w','$c','$p','$e')";        
        $exec=mysql_query($query,$connect);
        } else {
        echo "try again";
        }
        echo '</p>
<p align="center"><strong>Success</strong></p>
<p align="center"><a href="submit.php">Add another game</a> | <a href="index.php">Check game list ';
        } else {
        echo "<center><b>This page should only be accessed when submitting a game<b></center>";
        }
?>[/code]
Link to comment
Share on other sites

if ($error) {}
means if there is a value in $error then do what ever is between the brakcets

! means NOT.. therefore

if (!$error) {}

means if there is NO value to $error then do somthing

i think i did that wrong and should have used $_POST instead of $_POST[] in the foreach loop..
Link to comment
Share on other sites

if (isset($_POST['Submit'])) {


that is saying if the variable $_POST['Submit'] is set then do the following.. which $_POST stores everything from the boxes in your form

$post is just a temporary value to see if a value inside the form is blank
Link to comment
Share on other sites

[a href=\"http://infocenter.awardspace.com/inter/ps2ub/submit2.php\" target=\"_blank\"]http://infocenter.awardspace.com/inter/ps2ub/submit2.php[/a]
[a href=\"http://infocenter.awardspace.com/inter/ps2ub/insert.php\" target=\"_blank\"]http://infocenter.awardspace.com/inter/ps2ub/insert.php[/a]

insert.php:

[code]<?php    if (isset($_POST['Submit'])) {
           
        include 'db.php';

        $n=$_POST['textfield3'];
        $s=$_POST['radiobutton1'];
        $w=$_POST['radiobutton'];        
        $c=$_POST['textfield'];
        $p=$_POST['textfield2'];
        $e=$_POST['textfield22'];
        
        foreach($_POST as $post) {
        if ($post == '') {
        $error("Blank field detected"); //line 23
        }
        }
        
        if (!$error) {
        $query="INSERT INTO usbextreme (name,system,work_status,comments,postedby,email) VALUES             ('$n','$s','$w','$c','$p','$e')";        
        $exec=mysql_query($query,$connect);
        } else {
        echo "try again";
        }
        echo '</p>
<p align="center"><strong>Success</strong></p>
<p align="center"><a href="submit.php">Add another game</a> | <a href="index.php">Check game list ';
        } else {
        echo "<center><b>This page should only be accessed when submitting a game<b></center>";
        }
?>[/code]

Whats the problem?
Link to comment
Share on other sites

when submitting NO data i get error:
Fatal error: Call to undefined function: () in /home/www/infocenter.awardspace.com/inter/ps2ub/insert.php on line 23

well im confused on this one because error line is in red below yet nothing at all wrong with it!


} else {
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--] echo "try again";[!--colorc--][/span][!--/colorc--]
}


But if i submit data i get success..
Link to comment
Share on other sites

wow, i fixed it. i changed from $error("...") to $error="..." and then tested with different data, on different boxes and works fine, now the problem is that if there is one form to fill up it says try again, but I only want it to try again if one of the required fields arent filled up, they are the ones with stars *. How can i do that?

Ignore the success message as it always appear if the submit button is clicked.

One more thing, is there any code to make the browser return to a previous page without deleting the values written on the fields?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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