Jump to content


Photo

Required fields on a submit


  • Please log in to reply
15 replies to this topic

#1 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 01:25 PM

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.

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 April 2006 - 01:28 PM

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..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 01:31 PM

I try with an if statement, $var="", but it gave an error. How can I check for blank fields?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 April 2006 - 01:37 PM

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:
if(!isset($_POST['FieldName']) || empty($_POST['fileName']))
{
    echo "PLease fill in all fields thank you!;
}


#5 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 April 2006 - 01:51 PM

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 :)
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#6 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 02:46 PM

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

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 April 2006 - 02:49 PM

Please post the code that you're using.

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

Ken

#8 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 02:57 PM

   <?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>";
        }
?>


#9 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 April 2006 - 03:00 PM

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..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#10 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 03:02 PM

What does that $post and isset does?

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

#11 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 April 2006 - 03:34 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#12 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 03:56 PM

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?

#13 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 04:41 PM

[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:

<?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>";
        }
?>

Whats the problem?

#14 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 03 April 2006 - 04:52 PM

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..
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#15 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 05:19 PM

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?

#16 turbocueca

turbocueca
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 03 April 2006 - 06:35 PM

C'mon, how can I make a field NOT required?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users