Hi, I have a form that I’m just about done with, but I don’t know how to put PHp in a value tag that already ave SQL statements in it . First, I got a sticky form to work on a much smaller scale. I followed an example on https://www.youtube.com/watch?v=4Df0l9J-2i0 and I’m attaching the end result of the video. She explained things as she went and as can be noted, I made comments in the code to help me understand. After I was successful with that example, I took what I
understood from the video and tried to applied it to the form and I focused just on the fname variable for now. The thing that messing me up is since there is already SQL statements in the value tag, I’m not sure how to proceed. I’m now getting a Parse error: syntax error,
unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE
or T_NUM_STRING in /web/html/mediaservicesunlimited.com/contactUs.php on
line 86
error message
This is just one part of PHP that I really struggle with. Also, I realize most programmer are cringing because I’m mixing HTML and PHP. I apologize for that and I do understand why it is important to separate them now, but with this form it 's hard for me to understand
how to separate them.
if someone wouldn’t mind helping me understand how to make fname field to stick, I’m pretty sure I can finish the rest of the form. I’m not a strong programmer as anyone can tell, but if I could get help with this last part, I would be really appreciative.
sticky code sample
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Sticky Validation</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>A Simple Form for Sticky Validation</h2>
<?php
$nameError = "";
$emailError = "";
if($_SERVER['REQUEST_METHOD'] == 'POST'){
validateStuff();
}
else {
showForm($nameError, $emailError, $name, $Email); //add form variables
}
function validateStuff() {
//get variables from form submission
$name = $_POST['name'];
$email = $_POST['email'];
if(empty($name)){
$name = NULL; //change variable names for real form
$nameError = "<p>Please enter your first name.</p>";
}
if(empty($email)){
$email = NULL;
$emailError = "<p>Please enter your email address.</p>";
}
if(!($name && $email)){
showForm($nameError, $emailError, $name, $email); //Add variables
}
if($name && $email ){
//display an output message to user
print "Thanks for signing up for our mailing list <b> $name.</b>";
print "<p>We will send our newsletter to you at <b>$email</b>.</p>";
}
} //end .of validate function
function showForm($nameError, $emailError, $name, $email) { //add form variables
echo <<< FORM
<form method="post" action="">
<div>
<label for="name">*Name:</label>
<input name="name" type="text" id="name" placeholder="name" value = $name>
$nameError
</div>
<div class="clear">
<label for="email">*What is your email address?</label>
<input name="email" type="email" id="email" placeholder="email" value = $email >
$emailError
</div>
<div class="clear">
<input type="submit" value="Sign Up" name="submit" />
</div>
</form>
FORM;
}
?>
</body>
</html>
my contact page
<?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);
}
}
if(isset($_POST['submit']))
{
if(isset($_POST['projectOptions']))
{
$strprojectOptions = implode(",", $_POST['projectOptions']);
}
else {
$strprojectOptions = "";
}
//echo "interested in " . $strprojectOptions;
//exit(); //after testing remove these 2 lines
}
$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
$fname = ""; //start user's input validation
if($_SERVER['REQUEST_METHOD'] == 'POST'){
validateStuff();
}
else {
showForm($fname); //add form variables
}
function validateStuff() {
//get variables from form submission
$fname = $_POST['fname'];
if(empty($fname)){
$fname = NULL; //change variable names for real form
$nameError = "<p>Please enter your first name.</p>";
}
} //end of user's validation function
?>
<?php if(isset($sql_errors) && sizeof($sql_errors) > 0) outputErrors($sql_errors);?>
<?php
function showForm($fname, $nameError) { //Add form variables
echo <<<FORM
<form action="contactUs.php" method="post">
<label>
<input id="fname" type="text" name="fname" size="15" placeholder="First Name" value ="<?php echo isset($_POST['fname']) ? $_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>
FORM;
}
?>
</body>
</html>