Form doesnt post if somthing is not submitted.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Form doesnt post if somthing is not submitted.

Post by nickman013 »

I have a working form with javascript validation (checks to see if somthing is entered in fields before it is submitted).
But if people go to the processor page it will submit the form with nothing on it. Like it will just submit a blank form. Any way to prevent the form from entering if a value equals nothing.

I think it would be possible to do with this.

Code: Select all

if($name==''){
 die("you did not submit anything")
}
else { ... form processor script }
I dont know if that is exactly how it would be written, but that is my idea of how to do it. Thank You!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

huh? :roll:
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

haha, i dont know

i just dont want people going to the form processor page because it floods my email, i want to make a block on it, so there has to be somthing being sent for it to send? get what i mean?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if(empty($_POST['name'])){
   die("you did not submit anything")
}
else { 
   ... form processor script 
}
??
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

let me explain this a bit better

i have a form

Code: Select all

<form action=process.php action=post>
<input type=text name=name>
<input type=text name=comments>
<input type=text name=vote>
process.php

Code: Select all

<?
header('Location:/pages/report.php');
include("/home/muot/dbc.php");
mysql_query("update votes set number_of_votes = number_of_votes + 1 where answer = '$vote'");
$page = "thank you - voting.";
include('/home/muot/add2stat.php');
$content = "
<tr><td><b>$name</b> voted this muot as a $vote !<br>
<b>$name said $comments</td></tr>
";
$filename = '/home/muot/public_html/pages/report.php'; 
if (is_writable($filename)) { 
if (!$handle = fopen($filename, 'a')) { 
echo "Cannot open file ($filename)"; 
exit; 
} 
if (fwrite($handle, $content) === FALSE) { 
echo "Cannot write to file ($somecontent)"; 
exit; 
} 
echo ""; 
fclose($handle); 
} else { 
echo "The file $filename is not writable"; 
} 
?>
That works, just how I want it.

But if someone goes to http://www.mysite.com/process.php , it will write blank data to a page. I want to stop the script from doing anything if there is not any data inputed.

I tried to add your script but I got a parse error, unexpected } on line 4 , line 4 is }.

Thank You!
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Sounds like you have a syntax error. Check your { and your } and make sure they're all closed.

What you want to do is check to find out if you have a submission to the page. If there is process the page. If there isn't, don't process it. That's the concept.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

I have the form on a different page, and the processor on another.

I just dont want people being able to go to the processor page because it will just write data, and flood the page with no results.

Thank you
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

To add to my last post, I got it working. :D I added what Jcart told me to do, and it worked. The parse error was coming from the die("blah") , Jcart forgot to add a ; at the end. Its all good.

Thanks For the Help Guys!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

nickman013 wrote:To add to my last post, I got it working. :D I added what Jcart told me to do, and it worked. The parse error was coming from the die("blah") , Jcart forgot to add a ; at the end. Its all good.

Thanks For the Help Guys!
fyi I just copy and pasted your snipplet (and edited slightly).. :lol:
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

oh, lol sorry, i just thought you wrote it because the begining was a little different.

my mistake, sorry.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Nah don't be sorry, enjoy.
Post Reply