Jump to content

Recommended Posts

Hi, can someone help me understand why I'm getting this error please? 

 

Fatal error: Call to undefined function display_form() in /web/html/mediaservicesunlimited.com/test/stickyForm.php on line 33

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('functions.php');
function outputErrors($sql_errors)
{
 foreach($sql_errors as $name => $msgs)
 {
  echo('<h4 class="error">' . $name . ': ' . $msgs . '</h4>' . PHP_EOL);
 }
}
function validateInput()
{
  global $errors;
  $status = true;
  if($_POST['fname'] == "")
  {
   $errors['fname']="Please enter your first name";
   $status = false;
  }
  if($_POST['lname'] == "")
  {
   $errors['lname']="Please enter your last name";
   $status = false;
  }
  return $status;
}
if(isset($_POST['submit']))
{
 ValidateInput();
 if(count($errors) != 0)
 {
  display_form();
 }
 else
 {
  display_form();
 }
 $sql_errors = array();
 $mysqli = databaseConnection();
 if(!$stmt = $mysqli->prepare("INSERT INTO clients(fname, lname, orgName, address, city, state, zipcode, phone, fax, email, confirmEmail, projectOptions, projectOverview, year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
 {
  $sql_errors["Prepare"] = $mysqli->error . PHP_EOL;
 }
 else
 {
  if(!$stmt->bind_param('ssssssiiissisi', $_POST["fname"], $_POST["lname"], $_POST["orgName"], $_POST["address"], $_POST["city"], $_POST["state"], $_POST["zipcode"], $_POST["phone"], $_POST["fax"], $_POST["email"], $_POST["confirmEmail"], $_POST["projectOptions"], $_POST["projectOverview"], $_POST["year"]))
  {
   $sql_errors["bind_param"] = $stmt->error . PHP_EOL;
  }
  else
  {
   if(!$stmt->execute())
   {
    $sql_errors["execute"] = $stmt->error . PHP_EOL;
   }
   $stmt->close();
  }
 }
 $mysqli->close();
 header('contactTest.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
 .error
 {
  color: #FF0000;
 }
</style>
</head>
<body>
 <?php if(isset($sql_errors) && sizeof($sql_errors) > 0) outputErrors($sql_errors);?>
 <form action="stickyForm.php" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php if(isset($_POST['fname'])) echo $_POST['fname'];?>" >
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php echo !empty($error['lname']) ? $error['lname'] : '';?>
   <input type="text" name="orgName" placeholder="Organization's Name"maxlength="50">
  </label><br />
  <label> <!--new row -->
   <input id="address" type="text" name="address" size="15" placeholder="Street Addresss" maxlength="50">
   <input id="city" type="text" name="city" placeholder="City" size="10" maxlength="25">
   <select id="state" name="state" placeholder="State" value="">
    <option value ="">Please choose a state</option>
    <?php states($state); ?>
   </select>
   <input id = "zipcode" type="number" name="zipcode" placeholder="Zip Code" size="5" maxlength="5">
  </label><br />
  <label> <!--new row -->
   <input type="text" name="phone" placeholder="Phone Number:(including area code)" size="10" maxlength="10">
   <input type="text" name="fax" size="10" placeholder="Fax Number: (including area code)" maxlength="10">
  </label><br />
  <label> <!--new row-->
   <input type="text" id = "email" name="email" placeholder="Email Address" />
   <input type="text" id = "confirmEmail" name="confirmEmail" placeholder="Confirm Email Address" />
  </label><br />
  <label> <!--new row -->
   What would you like help with?
   <table id="projectOptions">
    <tr span=2>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="socialMedia">Social Media</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webContent">Web Content Management</td>
    </tr>
    <tr>
     <td><input name="projectOptions[]" type="checkbox" checked="checked" id="projectOptions[]" value="marketingMaterial">Marketing Material Creation</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="seo">SEO (Search Engine Optimization)</td>
    </tr>
    <tr>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="videoEditing"> Video Editing</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webDesign">Web Design</td>
    </tr>
   </table>
  </label>
  Overview about the project:<textarea rows="5" cols="10" placeholder="Overview of Project"></textarea><br />
  If you are not a robot, what year is it? <input type="text" name="year" size="4" maxlength="4"><br />
  <input type="submit" name="submit" value="Contact Me!">
  <input type="reset" name="reset" value="Cancel">
 </form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/299608-a-little-help-please/
Share on other sites

No, I'm sorry, I thought display_form() was a predefined function, sorry. 

 

 

No, sure isn't.  Keep in mind you can always use the php.net site.  It supports a rewrite for every function via  http://php.net/function_name.  So for example:

 

http://php.net/date_format

 

Worth trying when in doubt.

  • Like 1

Again, I apologize for my mistake.  I corrected that mistake and now the form won't display at all. it's just a blank page.  if someone could nicely point me in the right direction, I would really appreciate it. 

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('functions.php');
function outputErrors($sql_errors)
{
 foreach($sql_errors as $name => $msgs)
 {
  echo('<h4 class="error">' . $name . ': ' . $msgs . '</h4>' . PHP_EOL);
 }
}
$errors=array(); //validate user's input 
if(isset($_POST['submit']))
{
 validateInput();
 if(count($errors) != 0)
 {
  display_form();
 }
 else
 {
   display_form();
 }
 function validateInput()
{
  global $errors;
  $status = true;
  if($_POST['fname'] == "")
  {
   $errors['fname']="Please enter your first name";
   $status = false;
  }
  if($_POST['lname'] == "")
  {
   $errors['lname']="Please enter your last name";
   $status = false;
  }
  return $status;
} //end user validation 
 $sql_errors = array();
 $mysqli = databaseConnection();
 if(!$stmt = $mysqli->prepare("INSERT INTO clients(fname, lname, orgName, address, city, state, zipcode, phone, fax, email, confirmEmail, projectOptions, projectOverview, year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
 {
  $sql_errors["Prepare"] = $mysqli->error . PHP_EOL;
 }
 else
 {
  if(!$stmt->bind_param('ssssssiiissisi', $_POST["fname"], $_POST["lname"], $_POST["orgName"], $_POST["address"], $_POST["city"], $_POST["state"], $_POST["zipcode"], $_POST["phone"], $_POST["fax"], $_POST["email"], $_POST["confirmEmail"], $_POST["projectOptions"], $_POST["projectOverview"], $_POST["year"]))
  {
   $sql_errors["bind_param"] = $stmt->error . PHP_EOL;
  }
  else
  {
   if(!$stmt->execute())
   {
    $sql_errors["execute"] = $stmt->error . PHP_EOL;
   }
   $stmt->close();
  }
 }
 $mysqli->close();
 header('contactTest.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
 .error
 {
  color: #FF0000;
 }
</style>
</head>
<body>
 <?php if(isset($sql_errors) && sizeof($sql_errors) > 0) outputErrors($sql_errors);
 function display_form() {
global $errors; 
 ?>
<form action="stickyForm.php" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php if(isset($_POST['fname'])) echo $_POST['fname'];?>" >
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php echo !empty($error['lname']) ? $error['lname'] : '';?>
   <input type="text" name="orgName" placeholder="Organization's Name"maxlength="50">
  </label><br />
  <label> <!--new row -->
   <input id="address" type="text" name="address" size="15" placeholder="Street Addresss" maxlength="50">
   <input id="city" type="text" name="city" placeholder="City" size="10" maxlength="25">
   <select id="state" name="state" placeholder="State" value="">
    <option value ="">Please choose a state</option>
    <?php states($state); ?>
   </select>
   <input id = "zipcode" type="number" name="zipcode" placeholder="Zip Code" size="5" maxlength="5">
  </label><br />
  <label> <!--new row -->
   <input type="text" name="phone" placeholder="Phone Number:(including area code)" size="10" maxlength="10">
   <input type="text" name="fax" size="10" placeholder="Fax Number: (including area code)" maxlength="10">
  </label><br />
  <label> <!--new row-->
   <input type="text" id = "email" name="email" placeholder="Email Address" />
   <input type="text" id = "confirmEmail" name="confirmEmail" placeholder="Confirm Email Address" />
  </label><br />
  <label> <!--new row -->
   What would you like help with?
   <table id="projectOptions">
    <tr span=2>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="socialMedia">Social Media</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webContent">Web Content Management</td>
    </tr>
    <tr>
     <td><input name="projectOptions[]" type="checkbox" checked="checked" id="projectOptions[]" value="marketingMaterial">Marketing Material Creation</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="seo">SEO (Search Engine Optimization)</td>
    </tr>
    <tr>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="videoEditing"> Video Editing</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webDesign">Web Design</td>
    </tr>
   </table>
  </label>
  Overview about the project:<textarea rows="5" cols="10" placeholder="Overview of Project"></textarea><br />
  If you are not a robot, what year is it? <input type="text" name="year" size="4" maxlength="4"><br />
  <input type="submit" name="submit" value="Contact Me!">
  <input type="reset" name="reset" value="Cancel">
 </form>
<?php
 }
?>
</body>
</html>

First I'd suggest moving your function display_form() { .... } code to the top of the script right after your other function declaration and before your actual script logic code.

 

Re-run ad check your error logs and see what they are telling you.

I took your code and did the moving for you.  Once you start moving things into sections, the logic issues with your current code start to become more obvious. What's obvious at present is that the html header code needs to be either separated or moved inside a function so that it can be rendered before the form code is rendered OR before the error code is rendered.  

 

 

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('functions.php');
 
function outputErrors($sql_errors)
{
 foreach($sql_errors as $name => $msgs)
 {
  echo('<h4 class="error">' . $name . ': ' . $msgs . '</h4>' . PHP_EOL);
 }
}
 
function validateInput() {
  global $errors;
  $status = true;
  if($_POST['fname'] == "")
  {
   $errors['fname']="Please enter your first name";
   $status = false;
  }
  if($_POST['lname'] == "")
  {
   $errors['lname']="Please enter your last name";
   $status = false;
  }
  return $status;
} //end user validation 
 
function display_form() {
  global $errors; 
?>
<form action="stickyForm.php" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php if(isset($_POST['fname'])) echo $_POST['fname'];?>" >
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php echo !empty($error['lname']) ? $error['lname'] : '';?>
   <input type="text" name="orgName" placeholder="Organization's Name"maxlength="50">
  </label><br />
  <label> <!--new row -->
   <input id="address" type="text" name="address" size="15" placeholder="Street Addresss" maxlength="50">
   <input id="city" type="text" name="city" placeholder="City" size="10" maxlength="25">
   <select id="state" name="state" placeholder="State" value="">
    <option value ="">Please choose a state</option>
    <?php states($state); ?>
   </select>
   <input id = "zipcode" type="number" name="zipcode" placeholder="Zip Code" size="5" maxlength="5">
  </label><br />
  <label> <!--new row -->
   <input type="text" name="phone" placeholder="Phone Number:(including area code)" size="10" maxlength="10">
   <input type="text" name="fax" size="10" placeholder="Fax Number: (including area code)" maxlength="10">
  </label><br />
  <label> <!--new row-->
   <input type="text" id = "email" name="email" placeholder="Email Address" />
   <input type="text" id = "confirmEmail" name="confirmEmail" placeholder="Confirm Email Address" />
  </label><br />
  <label> <!--new row -->
   What would you like help with?
   <table id="projectOptions">
    <tr span=2>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="socialMedia">Social Media</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webContent">Web Content Management</td>
    </tr>
    <tr>
     <td><input name="projectOptions[]" type="checkbox" checked="checked" id="projectOptions[]" value="marketingMaterial">Marketing Material Creation</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="seo">SEO (Search Engine Optimization)</td>
    </tr>
    <tr>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="videoEditing"> Video Editing</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webDesign">Web Design</td>
    </tr>
   </table>
  </label>
  Overview about the project:<textarea rows="5" cols="10" placeholder="Overview of Project"></textarea><br />
  If you are not a robot, what year is it? <input type="text" name="year" size="4" maxlength="4"><br />
  <input type="submit" name="submit" value="Contact Me!">
  <input type="reset" name="reset" value="Cancel">
 </form>
<?php
}
 
$errors=array(); //validate user's input 
 
if(isset($_POST['submit']))
{
 validateInput();
 if(count($errors) != 0)
 {
  display_form();
 }
 else
 {
   display_form();
 }
 
 $sql_errors = array();
 $mysqli = databaseConnection();
 if(!$stmt = $mysqli->prepare("INSERT INTO clients(fname, lname, orgName, address, city, state, zipcode, phone, fax, email, confirmEmail, projectOptions, projectOverview, year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"))
 {
  $sql_errors["Prepare"] = $mysqli->error . PHP_EOL;
 }
 else
 {
  if(!$stmt->bind_param('ssssssiiissisi', $_POST["fname"], $_POST["lname"], $_POST["orgName"], $_POST["address"], $_POST["city"], $_POST["state"], $_POST["zipcode"], $_POST["phone"], $_POST["fax"], $_POST["email"], $_POST["confirmEmail"], $_POST["projectOptions"], $_POST["projectOverview"], $_POST["year"]))
  {
   $sql_errors["bind_param"] = $stmt->error . PHP_EOL;
  }
  else
  {
   if(!$stmt->execute())
   {
    $sql_errors["execute"] = $stmt->error . PHP_EOL;
   }
   $stmt->close();
  }
 }
 $mysqli->close();
 header('contactTest.php');
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
 .error
 {
  color: #FF0000;
 }
</style>
</head>
<body>
<?php if(isset($sql_errors) && sizeof($sql_errors) > 0) 
    outputErrors($sql_errors);
?>
</body>
 
</html>

Exactly.  As I said above, when you move the functions around it makes it clearer that you have logic holes in what you were doing.  

 

I didn't fix your code, just ordered it so that it wasn't so confusing and you could see clearly what you had.

A good model for script design is to set your php code all up front and your html all at the end.  They don't need to mix unless you are using php vars to convey html with dynamic embedded data into the final webpage output.

 

Keeps the logic and heavy work away from the presentation and display work.  No need to output intermittent dribs and drabs of html while you are trying to write the "serious" code.

I dont understand this at all.  I took out all the SQL code out temporary  so I could get just this part to work.  I'm following an example  from an old PHP book, but it is not working and I'm getting really frustrated.  When i try to display the form on the webpage, all I get is a blank page.  if someone could PLEASE help me, I would really appreciate it. 

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once('functions.php');
$errors=array(); //validate user's input 
 
if(isset($_POST['submit']))
{
 validateInput();
 if(count($errors) != 0)
 {
  display_form();
 }
 else
 {
   display_form();}

 function validateInput() {
  global $errors;
  if($_POST['fname'] == "")
  {
   $errors['fname']="Please enter your first name";
   
  }
  /*if($_POST['lname'] == "")
  {
   $errors['lname']="Please enter your last name";
   $status = false;
  }
  return $status;*/
 }} //end user validation
 ?>  
 <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">
 .error
 {
  color: #FF0000;
 }
</style>
</head>
<body>  
<?php 
 
function display_form() {
  global $errors; 
?>
<form action="stickyForm.php" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php echo $_POST[fname];//if(isset($_POST['fname'])) echo $_POST['fname'];?>" >
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php //echo !empty($error['lname']) ? $error['lname'] : '';?>
   <input type="text" name="orgName" placeholder="Organization's Name"maxlength="50">
  </label><br />
  <label> <!--new row -->
   <input id="address" type="text" name="address" size="15" placeholder="Street Addresss" maxlength="50">
   <input id="city" type="text" name="city" placeholder="City" size="10" maxlength="25">
   <select id="state" name="state" placeholder="State" value="">
    <option value ="">Please choose a state</option>
    <?php states($state); ?>
   </select>
   <input id = "zipcode" type="number" name="zipcode" placeholder="Zip Code" size="5" maxlength="5">
  </label><br />
  <label> <!--new row -->
   <input type="text" name="phone" placeholder="Phone Number:(including area code)" size="10" maxlength="10">
   <input type="text" name="fax" size="10" placeholder="Fax Number: (including area code)" maxlength="10">
  </label><br />
  <label> <!--new row-->
   <input type="text" id = "email" name="email" placeholder="Email Address" />
   <input type="text" id = "confirmEmail" name="confirmEmail" placeholder="Confirm Email Address" />
  </label><br />
  <label> <!--new row -->
   What would you like help with?
   <table id="projectOptions">
    <tr span=2>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="socialMedia">Social Media</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webContent">Web Content Management</td>
    </tr>
    <tr>
     <td><input name="projectOptions[]" type="checkbox" checked="checked" id="projectOptions[]" value="marketingMaterial">Marketing Material Creation</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="seo">SEO (Search Engine Optimization)</td>
    </tr>
    <tr>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="videoEditing"> Video Editing</td>
     <td><input type="checkbox" name="projectOptions[]" id="projectOptions[]" value="webDesign">Web Design</td>
    </tr>
   </table>
  </label>
  Overview about the project:<textarea rows="5" cols="10" placeholder="Overview of Project"></textarea><br />
  If you are not a robot, what year is it? <input type="text" name="year" size="4" maxlength="4"><br />
  <input type="submit" name="submit" value="Contact Me!">
  <input type="reset" name="reset" value="Cancel">
 </form>
<?php
}
?>  
</body>
 
</html>

I kind of got it working.  if the first name textbox is empty, but the last name is fliled in, it displays the error message but it does 't keep the last name input.  I thought the whole purpose of sticky forms is to keep the form data intact while showing what information is still needed. 

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
	$problem = false;
	if(isset($_POST['submit'])) {
		if(empty($_POST['fname'])) {
			$problem = true;
			echo "Please enter your first name"; 
		}
	}
?>   
<form action="sticky2.php" method="post">
  <label>
   <input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php echo $_POST[fname];//if(isset($_POST['fname'])) echo $_POST['fname'];?>" >
   <input type="text" name="lname" size="20" placeholder="Last Name"><?php //echo !empty($error['lname']) ? $error['lname'] : '';?>
</label>
  <input type="submit" name="submit" value="Contact Me!">
</form>   
</body>
</html>


I kind of got it working.  if the first name textbox is empty, but the last name is fliled in, it displays the error message but it does 't keep the last name input.  I thought the whole purpose of sticky forms is to keep the form data intact while showing what information is still needed.

 

The code for re-populating the last name field is commented out. Try using something like the following:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
 
<body>
<?php
$problem = false;
if($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(empty($_POST['fname'])) {
        $problem = true;
        echo "Please enter your first name"; 
    }
}
?>   
<form action="sticky2.php" method="post">
    <input type="text" name="fname" size="15" placeholder="First Name" value="<?php if(isset($_POST['fname'])) { echo $_POST['fname']; } ?>">
    <input type="text" name="lname" size="20" placeholder="Last Name" value="<?php  if(isset($_POST['lname'])) { echo $_POST['lname']; } ?>">
    <input type="submit" name="submit" value="Contact Me!">
</form>   
</body>
</html>

Note that I removed the <label> tag since you weren't really using it. To be honest, I'm not sure if the <label> tag is needed when you're using the "placeholder" attribute. Perhaps that's something else to research.  :happy-04: I also removed the "id" attribute from the first name field since it probably isn't being used.

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.