Jump to content

Recommended Posts

Hi, I haven't done PHP in years.  Could someone help me figure out why this code won't work please? 

 

there seems to be a problem between lines 24-28.

<?php
require_once('functions.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
	td {
		margin-bottom:110px;
	} 
</style>
<title>Untitled Document</title>
</head>
<body>
<table id="contactForm">
	<?Php
		$errors=array();
		if(isset($_POST['submit'])) {
			validate_input();
			if(count($errors) !=0) {
				display_form();
			}
		
			else(display_form();
			function validate_input() {
				global $errors;
				if($_POST["fname"] == * *) {
					$errors['fname']="<font color ='red'>
					Please enter your first name </font>";
				}
			
	?> 
	<form method="post">
		<tr>
        	<td class="question" >First Name:<br />
			<input type="text" name="fname" value =<?php echo $_POST[fname];?> size="15">	<?php echo $errors['fname']; ?></td>
	     	<td class="question">Last Name<br />
			<input type="text" name="lname" required size="20">	</td>
 	    	<td class="question">Organization's Name:<br />
			<input type="text" name="orgName" size="15" maxlength="50">	</td>
        </tr>            
        <tr>
         	<td class="question">Street Address: <br />
        	<input type="text" name="address" size="15" maxlength="50"></td>
	     	<td class="question">City: <br />
 			<input type="text" name="city" size="10" maxlength="25">	</td>
    	  	<td class="question">State:	<br />
        	 <select name = "state"  value="<?=$state?>">  <option value ="Please choose a state">
			Please choose a state</option>
            			<?php states($state) ?>
			</select>	  </td> 
         	<td class="question">Zipcode:<br />
        	<input type="number" name="zipcode" size="5" maxlength="5">	</td>    
        </tr>    
        <tr>
        	<td>Phone Number: <br />(including area code)  <br />
            <input type="text" name="phone" size="10" maxlength="10"></td>
			<td>Fax Number: <br />(including area code)	<br />
            <input type="text" name="fax" size="10" maxlength="10"></td>
        </tr>    
        <tr>
        	<td>Email:<br />
        	<input type="text" name="email" required ></td>
            <td>Confirm Email:<br />
        	<input type="text" name="ConfirmEmail" required ></td>
        </tr>    
        <tr>
        	<td>What would you like help with?  </td>
       			<td><table id="projectOptions">
        			<tr>
                		<td><input type="checkbox" name="SocialMedia">Social Media </td>	
                        <td><input type="checkbox" name="WebContentManagement">Web Content Management 	</td>
			        </tr>  
					<tr>
                		<td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation  </td>	
                        <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) 	</td>
                    </tr>
					<tr>
                		<td><input type="checkbox" name="VideoEditing"> Video Editing  </td>	
                        <td><input type="checkbox" name="WebDesign">Web Design  	</td>
			        </tr>  
                </table></td>
        <tr>
        	<td>Overview about the project:	</td>
        	<td><textarea></textarea></td>
        </tr>
        <tr>
        	<td>If you are not a robot, what year is it?  </td>    
            <td><input type="text" name="year" size="4" maxlength="4">
        </tr>
        <tr>
        	<td><input type="submit" name="Contact Me!"></td>
            <td><input type="reset"></td>
        </tr>
	</form>
</table>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/
Share on other sites

Do you think that may be because you haven't defined a function called display_form()?

 

And what is purpose of the if() statement here when you do exactly the same regardless of the outcome?

			if(count($errors) !=0) {
				display_form();
			}
		
			else display_form();

Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523964
Share on other sites

Strictly speaking, it ain't broken but a simple

display_form();

would do exactly the same as

            if(count($errors) !=0) {
                display_form();
            }
        
            else display_form();

The question is whether or not you want to call the function at all, as you don't appear to have a definition of that function. Should there be a display_form() function? Only you know that - we do not know what you intend to do.

Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523969
Share on other sites

I thought it was fixed, but if I dont put anything in the fname field and click submit no error message appears. 

<?php
require_once('functions.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
	td {
		margin-bottom:110px;
	} 
</style>
<title>Untitled Document</title>
</head>
<body>
<table id="contactForm">
	<?Php
		$errors=array();
		if(isset($_POST['submit'])) {
			validate_input();
			display_form(); 
			function validate_input() {
				global $errors;
				if($_POST["fname"] == " ") {
					$errors['fname']="<font color ='red'>
					Please enter your first name </font>";
				}
			}
		}
	?> 
	<form method="post">
		<tr>
        	<td class="question" >First Name:<br />
			<input type="text" name="fname" size="15" value =<?php echo $_POST[fname];?> >	<?php echo $errors['fname']; ?></td>
	     	<td class="question">Last Name<br />
			<input type="text" name="lname" size="20">	</td>
 	    	<td class="question">Organization's Name:<br />
			<input type="text" name="orgName" size="15" maxlength="50">	</td>
        </tr>            
        <tr>
         	<td class="question">Street Address: <br />
        	<input type="text" name="address" size="15" maxlength="50"></td>
	     	<td class="question">City: <br />
 			<input type="text" name="city" size="10" maxlength="25">	</td>
    	  	<td class="question">State:	<br />
        	 <select name = "state"  value="<?=$state?>">  <option value ="Please choose a state">
			Please choose a state</option>
            			<?php states($state); ?>
			</select>	  </td> 
         	<td class="question">Zipcode:<br />
        	<input type="number" name="zipcode" size="5" maxlength="5">	</td>    
        </tr>    
        <tr>
        	<td>Phone Number: <br />(including area code)  <br />
            <input type="text" name="phone" size="10" maxlength="10"></td>
			<td>Fax Number: <br />(including area code)	<br />
            <input type="text" name="fax" size="10" maxlength="10"></td>
        </tr>    
        <tr>
        	<td>Email:<br />
        	<input type="text" name="email" /></td>
            <td>Confirm Email:<br />
        	<input type="text" name="ConfirmEmail" /></td>
        </tr>    
        <tr>
        	<td>What would you like help with?  </td>
       			<td><table id="projectOptions">
        			<tr>
                		<td><input type="checkbox" name="SocialMedia">Social Media </td>	
                        <td><input type="checkbox" name="WebContentManagement">Web Content Management 	</td>
			        </tr>  
					<tr>
                		<td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation  </td>	
                        <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) 	</td>
                    </tr>
					<tr>
                		<td><input type="checkbox" name="VideoEditing"> Video Editing  </td>	
                        <td><input type="checkbox" name="WebDesign">Web Design  	</td>
			        </tr>  
                </table></td>
        <tr>
        	<td>Overview about the project:	</td>
        	<td><textarea></textarea></td>
        </tr>
        <tr>
        	<td>If you are not a robot, what year is it?  </td>    
            <td><input type="text" name="year" size="4" maxlength="4">
        </tr>
        <tr>
        	<td><input type="submit" value="Contact Me!"></td>
            <td><input type="reset"></td>
        </tr>
	</form>
</table>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523977
Share on other sites

You have coded it to give an error only when the name contains a single space

 
            if($_POST["fname"] == " ") {
                    $errors['fname']="<font color ='red'>
                    Please enter your first name </font>";
                }
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523997
Share on other sites

Use empty()

if(empty($_POST['fname'])){
     $errors['fname'] = "<span class='error'>Please enter your first name.</span>";
}

Please note that I updated your code to HTML5 and assumed you would use stylesheets, instead of deprecated markup and inline styling as is currently the case.

Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524029
Share on other sites

I'm so sorry.  I put what you wrote but it 's still not working if the fname field is left blank. 

<?Php
		$errors=array();
		if(isset($_POST['submit'])) {
			validate_input();
			display_form(); 
			function validate_input() {
				global $errors;
				if(empty($_POST['fname'])){
			     $errors['fname'] = "<span class='error'>Please enter your 				                      first name.</span>";
                  }
			}
		}
	?> 
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524043
Share on other sites

Try running this code - comment out everything else on your page, paste this at the top of the page, and let us know what it says in your browser when you submit the form to this page.

<?php
error_reporting(-1);
ini_set('display_errors',1);

if(isset($_POST['submit'])) {
	$errors = validate_input();
	display_form($errors); 
}else{
	print("<p>POST is not set</p>");
}

function validate_input() {
	if(empty($_POST['fname'])){
		 $errors['fname'] = "<span class='error'>Please enter your first name.</span>";
	}
	return $errors;
}

function display_form(array $errors){
	if(!empty($errors)){
		print("<pre>".print_r($errors,true)."</pre>");
	}else{
		print("<p>No errors!</p>");
	}
}
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524051
Share on other sites

At the top of the page it says POST is not set and under the first name textbox it says Notice: Undefined variable: errors in /web/html/mediaservicesunlimited.com/contactUs.php on line 58

<?php
error_reporting(-1);
ini_set('display_errors',1);

if(isset($_POST['submit'])) {
	$errors = validate_input();
	display_form($errors); 
}else{
	print("<p>POST is not set</p>");
}

function validate_input() {
	if(empty($_POST['fname'])){
		 $errors['fname'] = "<span class='error'>Please enter your first name.</span>";
	}
	return $errors;
}

function display_form(array $errors){
	if(!empty($errors)){
		print("<pre>".print_r($errors,true)."</pre>");
	}else{
		print("<p>No errors!</p>");
	}
}

//<?php
require_once('functions.php');
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">

</style>
<title>Untitled Document</title>
</head>
<body>
<table id="contactForm" border cellspacing="20"> 
<?php /*
		$errors=array();
		if(isset($_POST['submit'])) {
			validate_input();
			display_form(); 
			function validate_input() {
				global $errors;
				if(empty($_POST['fname'])){
			     $errors['fname'] = "<span class='error'>Please enter your 				                      first name.</span>";
                  }
			}
		}
	?> 
<?php validate_input();*/ ?>  
	<form method="post">
		<tr>
        	<td class="question" >First Name:<br />
			<input type="text" name="fname" size="15" value =<?php //aecho $_POST[fname];?> >	<?php echo $errors['fname']; ?></td>
	     	<td class="question">Last Name<br />
			<input type="text" name="lname" size="20">	</td>
 	    	<td class="question">Organization's Name:<br />
			<input type="text" name="orgName" size="15" maxlength="50">	</td>
        </tr>            
        <tr>
         	<td class="question">Street Address: <br />
        	<input type="text" name="address" size="15" maxlength="50"></td>
	     	<td class="question">City: <br />
 			<input type="text" name="city" size="10" maxlength="25">	</td>
    	  	<td class="question">State:	<br />
        	 <select name = "state"  value="<?=$state?>">  <option value ="Please choose a state">
			Please choose a state</option>
            			<?php states($state); ?>
			</select>	  </td> 
         	<td class="question">Zipcode:<br />
        	<input type="number" name="zipcode" size="5" maxlength="5">	</td>    
        </tr>    
        <tr>
           	<td>Phone Number: <br />(including area code)  <br />
            <input type="text" name="phone" size="10" maxlength="10"></td>
			<td>Fax Number: <br />(including area code)	<br />
            <input type="text" name="fax" size="10" maxlength="10"></td>
        </tr>    
        <tr>
        	<td>Email:<br />
        	<input type="text" name="email" /></td>
            <td>Confirm Email:<br />
        	<input type="text" name="ConfirmEmail" /></td>
        </tr>    
        <tr>
        	<td>What would you like help with?  </td>
       			<td><table id="projectOptions">
        			<tr span=2>
                		<td><input type="checkbox" name="SocialMedia">Social Media </td>	
                        <td><input type="checkbox" name="WebContentManagement">Web Content Management 	</td>
			        </tr>  
					<tr>
                		<td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation  </td>	
                        <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) 	</td>
                    </tr>
					<tr>
                		<td><input type="checkbox" name="VideoEditing"> Video Editing  </td>	
                        <td><input type="checkbox" name="WebDesign">Web Design  	</td>
			        </tr>  
                </table></td>
        <tr>
        	<td>Overview about the project:	</td>
        	<td><textarea></textarea></td>
        </tr>
        <tr>
        	<td>If you are not a robot, what year is it?  </td>    
            <td><input type="text" name="year" size="4" maxlength="4">
        </tr>
        <tr>
        	<td><input type="submit" value="Contact Me!"></td>
            <td><input type="reset"></td>
        </tr>
	</form>
</table>
</body>
</html>
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524098
Share on other sites

Your problem is that submit is missing the name attribute

 

Your code:

<input type="submit" name="submit" value="Contact Me!">   

 

Correct:

<input type="submit" name="submit" value="Contact Me!">   

 

 

You still have other issues. I may rewrite it for you.

 

You are also missing a couple closing tags and you dont use tables for page layout. Use CSS.

Edited by benanamen
Link to comment
https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524101
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.