Jump to content

Archived

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

turbocueca

Required fields on a submit

Recommended Posts

When submiting information through a PHP page to a mysql database, how can I make a field required? It must have any information or the user won't be able to submit any info.

Share this post


Link to post
Share on other sites
Your looking at javascript not PHP for this mate try posting in the javascript forum :)

Only thing with PHP you can do is when the page is submitted check for blank fields..

Share this post


Link to post
Share on other sites
I try with an if statement, $var="", but it gave an error. How can I check for blank fields?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Please post the code that you're using.

The superglobal array that contains the values from a submitted form is $_POST, not $post.

Ken

Share this post


Link to post
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]

Share this post


Link to post
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..

Share this post


Link to post
Share on other sites
What does that $post and isset does?

I can't test the code right now, the server is having some probs..

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Fatal error: Call to undefined function: () in /home/www/infocenter.awardspace.com/inter/ps2ub/insert.php on line 23

i removed those []

anyway, what's foreach?

Share this post


Link to post
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?

Share this post


Link to post
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..

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

×

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.