Jump to content

Recommended Posts

hey guys i'm having trouble with this sticky form i coded...my form don't seem to show when i run the script.

 

here's my code:

<?php

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

if(!empty($_POST['fname']))
{

print"<b>First name:</b>{$_POST['fname']}";
}
  else
  {
  
  print "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>";
  include('form.php');

   }
   

if(!empty($_POST['lname']))
{
print"<b>Last name:</b>{$_POST['lname']}";
}
else
{
print "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>";
include('form.php');	 	

}

if(!empty($_POST['email']))
{
print"<b>Email:</b>{$_POST['email']}";
}
else
{  
print "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>";
include('form.php');

}

if(!empty($_POST['ctn']))
{
  print"<b>Contact number:</b>{$_POST['ctn']}";
}
  
else
{
  
print "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>";
include('form.php');

}
} 
?>

 

thats my index.php

 

here's my form.php

<form name="basic" action="index.php" method="post">

<table cellpadding="8" style="text-align:left;color:black;font-family:tahoma;font-size:12;padding:20px;">

<tr><td>First name:</td><td><input type='text' name='fname' value='<?php if(isset($_POST['fname'])) print $_POST['fname'];?>'</input></td></tr>
<tr><td>Last name:</td><td><input type='text' name='lname' value='<?php if(isset($_POST['lname'])) print $_POST['lname'];?>'</input></td></tr>
<tr><td>Email:</td><td><input type='text' name='email' value='<?php if(isset($_POST['email'])) print $_POST['email'];?>'</input></td></tr>
<tr><td>Contact number:</td><td><input type='text' name='ctn' value='<?php if(isset($_POST['ctn'])) print $_POST['ctn'];?>'</input></td></tr>

<input type="hidden" name="submitted" value="TRUE"/>

<tr><td><input type="submit" value="Submit"></td></tr>

</form>

 

please help

 

 

i've coded it differently

<?php

if(isset($_POST['submitted']))
{
$problem=FALSE;

if(empty($_POST['fname']))
{

print "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>";
	$problem=TRUE;
}
  

if(empty($_POST['lname']))
{

print "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>";
	$problem=TRUE;

}

if(empty($_POST['email']))
{

print "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>";
	$problem=TRUE;

}

if(empty($_POST['ctn']))
{

print "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>";
	$problem=TRUE;

}
else
{
print"please try again";

}
} 
?>

 

it gets all tangled up, don't know how to fix this..

 

please help..

Link to comment
https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/
Share on other sites

try

 

if(isset($_POST['submitted']))

{

$problem=FALSE;

     

else if(empty($_POST['fname']))

{

 

print "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>";

      $problem=TRUE;

}

 

 

else if(empty($_POST['lname']))

{

 

print "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>";

      $problem=TRUE;

     

}

 

else if(empty($_POST['email']))

{

 

print "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>";

      $problem=TRUE;

     

}

 

else if(empty($_POST['ctn']))

{

 

print "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>";

      $problem=TRUE;

     

}

else

{

print"please try again";

 

}

}

?>

[code=php:0]

the last else you wrote is only connected to the last if, and not the entire chain of if statements. I'm still not sure what you are trying to do with this form, or what the problem is. Can you explain further?

Hey, thanx buddy, but its still not working...The first index.php page that i posted above is the more preferable one or one that might work if it gets  chop and changed a bit.. I want the user to enter his first name, last name, email adres and contact number.. if one field is left out a warning message should appear and the form should be displayed again already containing the values of the fields he/she did complete – thus a ‘sticky’ form.

 

my form does not display, the include function seems to not be working.. i'm not really good at php tho, so i might be messing up somewhere.

you're missing an end table tag, btw.

 

try this :

<?php
error_reporting(E_ALL);
if(isset($_POST['Submit'])) //lost the hidden input .. just use the submit button instead;
{   
if(!isset($_POST['fname']))
{ $errors = "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>"; }
if(!isset($_POST['lname']))
{ $errors[] = "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>"; }
if(!isset($_POST['email']))
{ $errors[] = "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>"; }
if(!isset($_POST['ctn']))
{ $errors[] = "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>"; }

#check if there are any errors;
if (is_array ($errors))
{ $do = "errors"; }
else
{ $do = "submit"; }

/* switch();
 * toggle errors, valid submission;
 * default: show the form;
 */
switch ($do)
{
	case errors:
		$i = 1;
		foreach ($errors as $v)
		{
			echo $i++.'. '.$v.'<br />';
		}
		include ('form.php');
	break;
	case submit:
		//do any required actions here, ie. insert/update database, send email, etc.
	break;
	default:
		include ('form.php');
	break;
}
} 
?>

keep your form as is .. looks like it should remain sticky.

 

post your results afterwards.

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.