Jump to content
emcy

Empty records created in mysqli via my php script from html form.

Recommended Posts

Hi, I have spent hours on this and not figured it out. researched online extensively, and I'm sure I'm not the first one , but I haven't been able to figure  it out.  Help please?

I have an action_page1.php script.  I have an html contact form.  I have a msqli db setup.

The form looks ok on the html in the browser.  

The form seems to work alright when I enter the information, in so much that it seems to take the info and spit out a the return screen that says: 

"Connection to server successfully established. 
New record created successfully. Last inserted ID is: 9First Name: 
Last Name: 
Phone: 
Address: 
City: 
State: 
Zip: 
Email Address: 
Country: 
Comments: 
Subscribe to Mailing List?: 
Error connecting to database."

 

Issues: 

1. I don't know why it says "Error" connecting to database" at the end. huh? I know I programmed it in there, but I'm not sure why it's showing up there if the top says it's fine?!

2. How do I get the 'First Name" to sit under the ID instead of next to it?

3. When I go to see the record in the database, it is empty...why?

here is a snap shot of what the records looks like:

https://drive.google.com/open?id=1gPZEosBsouyo5hrkI_u4ZzB5Xanh35Vr

4. Why are the fields in the returned screen above, empty also?

here is the  script file:

"<?php

$host_name  = "db.......";

$database   = "db......";

$user_name  = "db......";

$password   = "..........";

$connect = mysqli_connect($host_name, $user_name, $password, $database);
    
 if(mysqli_connect_errno())
    {
    echo '<p>Failed to connect to Server: '.mysqli_connect_error().'</p>';

}
    else
    {
    echo '<p>Connection to server successfully established.</p>';

}

$mysqli = "INSERT INTO contactpage (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
VALUES ('$first_name','$last_name','$phone_number','$address','city','state','zip','$country','$email_address', '$comments','$join_mailing_list')";


if ($connect->query($mysqli) === TRUE) {
    $last_id = $connect->insert_id;
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $mysqli . "<br>" . $connect->error;
}
// define variables and set to empty values
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone_number = $_POST['phone_number'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$email_address = $_POST['email_address'];
$country = $_POST['country'];
$comments = $_POST['comments'];
$join_mailing_list = $_POST['join_mailing_list'];

$first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";

$email = ".............";
$emailSubject = "test";

$headers = "From:$emailaddress\r\n";
$headers .= "Content-type: text/html\r\n";

$body = <<<EOD
<br><hr><br>
First Name : $first_name <br>
Last Name : $last_name <br>
Phone Number : $phone_number <br>
Address : $address <br>
City : $city <br>
State : $state <br>
Zip : $zip <br>
Country : $country <br>
Email Address : $email_address <br>
Comments : $comments <br>
Subscribe to Mailing List : $join_mailing_list <br>
EOD;

$success = mail($email, $emailSubject, $body, $headers);

$theResults = <<<EOD
"Thank you. Your message has been sent and you will be contacted soon"
echo "<br>";
echo "<h2>Your Input:</h2>";
echo  $first_name;
echo "<br>";
echo  $last_name;
echo "<br>";
echo  $phone_number;
echo "<br>";
echo  $address;
echo "<br>";
echo  $city;
echo "<br>";
echo  $state;
echo "<br>";
echo  $zip;
echo "<br>"
echo  $country;
echo "<br>";
echo  $email_address;
echo "<br>";
echo  $comments;
echo "<br>";
echo  $join_mailing_list;
echo "<br>";
EOD;

echo "First Name:  " . $first_name . "<br>";
echo "Last Name:  " . $last_name . "<br>";
echo "Phone:  " . $phone_number . "<br>";
echo "Address:  " . $address . "<br>";
echo "City:  " . $city . "<br>";
echo "State:  " . $state . "<br>";
echo "Zip:  " . $zip . "<br>";
echo "Email Address:  " . $email_address . "<br>";
echo "Country:  " . $country . "<br>";
echo "Comments: " . $comments . "<br>";
echo "Subscribe to Mailing List?:  " . $join_mailing_list . "<br>";


$db = mysqli_connect("localhost","db..........",".........");
if(!$db) die("Error connecting to database.");
mysqli_select_db("db.........." ,$db);

$mysqli = "INSERT INTO contactpage (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list) 
VALUES (".
PrepmySQLi($varfirst_name) . ", " .
PrepmySQLi($varlast_name) . ", " .
PrepmySQLi($varphone_number) . ", " .
PrepmySQLi($varaddress) . ", " .
PrepmySQLi($varcity) . ", " .
PrepmySQLi($varstate) . ", " .
PrepmySQLi($varzip) . ", " .
PrepmySQLi($varcountry) . ", " . 
PrepmySQLi($varemail_address) . ", " .
PrepmySQLi($varcomments) . ", " .
PrepSQL($varjoin_mailing_list) . ", " .

mysqli_query($mysqli);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["first_name"])) {
    $first_nameErr = "First name is required";
  } else {
    $first_name = test_input($_POST["first_name"]);
  if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
    $first_nameErr = "Only letters and white space allowed"; 
}
}
  if (empty($_POST["last_name"])) {
    $last_nameErr = "Last name is required";
  } else {
    $last_name = test_input($_POST["last_name"]);
  if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
    $last_nameErr = "Only letters and white space allowed"; 
}
}
  if (empty($_POST["phone_number"])) {
    $phone_numberErr = "Please enter a phone number";
  } else {
    $phone_number = test_input($_POST["phone_number"]);
}
  if (empty($_POST["address"])) {
    $addressErr = "Address is required";
  } else {
    $address = test_input($_POST["address"]);
}

if (empty($_POST["city"])) {
    $cityErr = "City is required";
  } else {
    $city = test_input($_POST["city"]);
}
if (empty($_POST["state"])) {
    $stateErr = "State is required";
  } else {
    $state = test_input($_POST["state"]);
}

if (empty($_POST["zip"])) {
    $zipErr = "Zip is required";
  } else {
    $zip = test_input($_POST["zip"]);
}

 if (empty($_POST["country"])) {
    $countryERR = "please enter your country";
   } else {
   $country = test_input($_POST["country"]);
}

  if (empty($_POST["email_address"])) {
    $email_addressErr = "Email is required";
  } else {
    $email_address = test_input($_POST["email_address"]);
  if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    $email_addressErr = "Invalid email format";
}
}
 
  if (empty($_POST["comments"])) {
    $comments = "";
  } else {
    $comments = test_input($_POST["comments"]);
}
  if (empty($_POST["join_mailing_list"])) {
    $join_mailing_listErr = "requres a yes or no";
  } else {
    $join_mailing_list = test_input($_POST["join_mailing_list"]);
}
 
 function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

  if($_POST['formSubmit'] == "Submit") 
{
$varfirst_name = $_POST['first_name'];
$varlast_name = $_POST['last_name'];
$varphone_number = $_POST['phone_number'];
$varaddress = $_POST['address'];
$varcity = $_POST['city'];
$varstate = $_POST['state'];
$varzip = $_POST['zip'];
$varcountry = $_POST['country'];
$varemail_address = $_POST['email_address'];
$varcomments = $_POST['comments'];
$varjoin_mailing_list = $_POST['join_mailing_list'];
$errorMessage = "";
// - - - snip - - - 
}

echo "$theResults";
}
$connect->close();
?>"
 

Thanks for any help you can offer!  
 

 

 

 

Share this post


Link to post
Share on other sites

1. I don't know why it says "Error" connecting to database" at the end. huh? I know I programmed it in there, but I'm not sure why it's showing up there if the top says it's fine?!

I'm not sure why you're connecting to the database more than once, but I bet you're not doing it right the last time.  You don't need to connect more than once in the vast majority of situations though.

2. How do I get the 'First Name" to sit under the ID instead of next to it?

In HTML you use <br> to add a line break.

3. When I go to see the record in the database, it is empty...why?

You're running the query before you define any variables or get any form data.  You should add this at the top of your code:

ini_set('display_errors', 1);
	error_reporting(E_ALL);

You also really need to use prepared statements instead of putting variables directly in a SQL query, in most situation you should not use variables in a SQL query.  Use prepared statements for that, mysqli supports them and so does PDO.

4. Why are the fields in the returned screen above, empty also?

That's the same question as above.  If you print that insert query to see exactly what you're telling the database to do, you'll notice it has no data in it.  If you add that code to enable all error messages you'll see why.

It will also be a lot easier to read your code if you post it in a code box instead of as regular text.  The editor has a button for a code box.

Share this post


Link to post
Share on other sites

Whoa! Lots of error messages when I added that tidbit of code. Thanks!  I have some more work to do .  Will check back in soon...

 

Share this post


Link to post
Share on other sites

Hi, I love that little code! Thank you!..  I tried it wow!

I cleaned up most of it.

Here is how it now stands...

  • There are no new records created in the database,
  • the user doesn't get his copy of what he submitted,
  • the email sent to me only contains the fields but not the user's info,
  • the "Thank you your message was received successfully. Here is a copy of what you sent us:" message is on the page all the time from the moment the page is called up,
  • the user's info doesn't echo back onto the page for them to see.

While, I understand this page is doing exactly as it is programmed to  LOL!,  It's not doing what I thought I programmed it to do! LOL

This first part about the Undefined variables (line 21 and 24)... I had it originally set to $sql and got the same error message. so then I thought it's because it should be mysqli. So I changed it and got the same error message. ugh

Also I have searched the internet for info as to what the "Undefined index" could mean... I thought perhaps I did not have the settings correct in mysqli in the columns. so I went in and added an index for each column, but I still get the same error messages. Then I think I am done thinking ;) I thought perhaps there is a simple typo or an equal sign missing and I've checked them all and could not find it. help please?

I am getting the following error messages (I have posted the code below these, in a box as requested. Thanks for pointing that out :) : )

 

Thanks for anyone's help please!

 

Notice: Undefined variable: mysqli in /homepages/42/htdocs/contactcode.php on line 21
Warning: mysqli_query(): Empty query in /homepages/42/htdocs/contactcode.php on line 21
Notice: Undefined variable: mysqli in /homepages/42/htdocs/contactcode.php on line 24
Error:
Notice: Undefined index: first_name in /homepages/42/htdocs/contactcode.php on line 30
Notice: Undefined index: last_name in /homepages/42/htdocs/contactcode.php on line 31
Notice: Undefined index: phone_number in /homepages/42/htdocs/contactcode.php on line 32
Notice: Undefined index: address in /homepages/42/htdocs/contactcode.php on line 33
Notice: Undefined index: city in /homepages/42/htdocs/contactcode.php on line 34
Notice: Undefined index: state in /homepages/42/htdocs/contactcode.php on line 35
Notice: Undefined index: zip in /homepages/42/htdocs/contactcode.php on line 36
Notice: Undefined index: email_address in /homepages/htdocs/contactcode.php on line 37
Notice: Undefined index: country in /homepages/42/htdocs/contactcode.php on line 38
Notice: Undefined index: comments in /homepages/42/htdocs/contactcode.php on line 39
Notice: Undefined index: join_mailing_list in /homepages/42/htdocs/contactcode.php on line 40
Warning: mysqli_close(): Couldn't fetch mysqli in /homepages/42/htdocs/contactcode.php on line 205

8 // create the connection
9 $connect = mysqli_connect($host_name, $user_name, $password, $database);
10 
11 // check the connection
12 
13 if (!$connect) {
14    die("Connection failed: " . mysqli_connect_error());
15 }
16 //keep this in here to display all the errors on debugging
17 ini_set('display_errors', 1);
18 	error_reporting(E_ALL);
19 //this is where the debugging portion ends
20
21 if (mysqli_query($connect, $mysqli)) {
22    echo "New record created successfully";
23 } else {
24    echo "Error: " . $mysqli . "<br>" . mysqli_error($connect);
25 }
26
27 mysqli_close($connect);
28
29 // define variables and set to empty values
30 $first_name = $_POST['first_name'];
31 $last_name = $_POST['last_name'];
32 $phone_number = $_POST['phone_number'];
33 $address = $_POST['address'];
34 $city = $_POST['city'];
35 $state = $_POST['state'];
36 $zip = $_POST['zip'];
37 $email_address = $_POST['email_address'];
38 $country = $_POST['country'];
39 $comments = $_POST['comments'];
40 $join_mailing_list = $_POST['join_mailing_list'];
41
42 $first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";
43 $first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $join_mailing_list = "";
44 
45 $mysqli = "INSERT INTO nydatabase (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
46 VALUES ('$first_name','$last_name','$phone_number','$address','city','state','zip','$country','$email_address', '$comments','$join_mailing_list')";
47
48
49 if ($_SERVER["REQUEST_METHOD"] == "POST") {
50   if (empty($_POST["first_name"])) {
51     $first_nameErr = "First name is required";
52  } else {
53    $first_name = test_input($_POST["first_name"]);
54  if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {
55    $first_nameErr = "Only letters and white space allowed"; 
56 }
57 }
58  if (empty($_POST["last_name"])) {
59    $last_nameErr = "Last name is required";
60  } else {
61    $last_name = test_input($_POST["last_name"]);
62  if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
63    $last_nameErr = "Only letters and white space allowed"; 
64 }
65 }
66 if (empty($_POST["phone_number"])) {
67    $phone_numberErr = "Please enter a phone number";
68  } else {
69    $phone_number = test_input($_POST["phone_number"]);
70 }
71  if (empty($_POST["address"])) {
72   $addressErr = "Address is required";
73  } else {
74    $address = test_input($_POST["address"]);
75 }
76
77 if (empty($_POST["city"])) {
78    $cityErr = "City is required";
79  } else {
80    $city = test_input($_POST["city"]);
81	if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
82    $cityErr = "Only letters and white space allowed"; 
83 }
84 }
85 if (empty($_POST["state"])) {
86    $stateErr = "State is required";
87  } else {
88    $state = test_input($_POST["state"]);
89	if (!preg_match("/^[a-zA-Z ]*$/",$state)) {
90    $stateErr = "Only letters and white space allowed"; 
91 }
92 }
93
94 if (empty($_POST["zip"])) {
95    $zipErr = "Zip is required";
96   } else {
97    $zip = test_input($_POST["zip"]);
98 }
99
100 if (empty($_POST["country"])) {
101   $countryERR = "please enter your country";
102   } else {
103   $country = test_input($_POST["country"]);
104   if (!preg_match("/^[a-zA-Z ]*$/",$country)) {
105    $countryErr = "Only letters and white space allowed"; 
106 }
107 }
108
109  if (empty($_POST["email_address"])) {
110    $email_addressErr = "Email is required";
111  } else {
112    $email_address = test_input($_POST["email_address"]);
113  if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
114    $email_addressErr = "Invalid email format";
115 }
116 }
117 
118  if (empty($_POST["comments"])) {
119    $comments = "";
120  } else {
121    $comments = test_input($_POST["comments"]);
122 }
123   if (empty($_POST["join_mailing_list"])) {
124    $join_mailing_listErr = "requres a yes or no";
125  } else {
126    $join_mailing_list = test_input($_POST["join_mailing_list"]);
127 }
128 
129  function test_input($data) {
130   $data = trim($data);
131   $data = stripslashes($data);
132  $data = htmlspecialchars($data);
133  return $data;
134 }
135 }
136 ?>
137 <p><span class="error">* required field.</span></p>
138 <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="text/plain">
139        First Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="First Name" required name="first_name" value="<?php echo $first_name;?>"><span class="error">* <?php echo $first_nameErr;?></span><br>
140        Last Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Last Name" required name="last_name" value="<?php echo $last_name;?>"><span class="error">* <?php echo $last_nameErr;?></span><br>
141        Phone:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Phone" required name="phone_number" size="50" value="<?php echo $phone_number;?>"><span class="error">* <?php echo $phone_numberErr;?></span><br>
142        Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Address" required name="address" value="<?php echo $address;?>"><span class="error">* <?php echo $addressErr;?></span><br>
143        City:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="City" required name="city" value="<?php echo $city;?>"><span class="error">* <?php echo $cityErr;?></span><br>
144        State:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="State" required name="state" value="<?php echo $state;?>"><span class="error">* <?php echo $stateErr;?></span><br>
145        Zip Code:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Zip" required name="zip" value="<?php echo $zip;?>"><span class="error">* <?php echo $zipErr;?></span><br>
146        Country:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Country" required name="country" value="<?php echo $country;?>"><span class="error">* <?php echo $countryErr;?></span><br>
147        Email Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Email Address" required name="email_address" value="<?php echo $email_address;?>"><span class="error">* <?php echo $email_addressErr;?></span><br>
148        Comment:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Message" required name="comments" size="75" value="<?php echo $comments;?>"><span class="error">* <?php echo $commentsErr;?></span><br>
149        Would you like to be on our mailing list?<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list=="yes") echo "checked";?>
150 value="yes"><label>Yes</label>
151		<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list=="no") echo "checked";?>
152 value="no"><label>No</label><span class="error">* <?php echo $join_mailing_listErr;?></span>
153 <br>
154        <input class="w3-input w3-padding-16 w3-border" type="datetime-local" placeholder="Date and time" required name="date" value="2017-11-16T20:00"><br>
155        <button class="w3-button w3-black"><input type="submit" name="submit" value="Submit"></button>
156      </form>
157
158 <?php
159 $email = "me@myemail.com";
160 $emailSubject = "Inquiry from the Contact Page";
161
162 $headers = "From:$email_address\r\n";
163 $headers .= "Content-type: text/html\r\n";
164
165 $body = <<<EOD
166 <br><hr><br>
167 First Name : $first_name <br>
168 Last Name : $last_name <br>
169 Phone Number : $phone_number <br>
170 Address : $address <br>
171 City : $city <br>
172 State : $state <br>
173 Zip : $zip <br>
174 Country : $country <br>
175 Email Address : $email_address <br>
176 Comments : $comments <br>
177 Subscribe to Mailing List : $join_mailing_list <br>
178 EOD;
179
180 $success = mail($email, $emailSubject, $body, $headers);
181 $usersuccess = mail($email_address, $emailSubject, $body, $headers);
182 echo "<h2>Thank you your message was received successfully. Here is a copy of what you sent us:</h2>";
183 echo $first_name;
184 echo "<br>";
185 echo $last_name;
186 echo "<br>";
187 echo $phone_number;
188 echo "<br>";
189 echo $address;
190 echo "<br>";
191 echo $city;
192 echo "<br>";
193 echo $state;
194 echo "<br>";
195 echo $zip;
196 echo "<br>";
197 echo $country;
198 echo "<br>";
199 echo $email_address;
200 echo "<br>";
201 echo $comments;
202 echo "<br>";
203 echo $join_mailing_list;
204
205 mysqli_close($connect);
206 ?>   

 

Edited by emcy

Share this post


Link to post
Share on other sites

The php is read from top to bottom, on line 21 to refers to $mysqli variable

if (mysqli_query($connect, $mysqli))

, but this variable is declared at line line 45

$mysqli = "INSERT INTO nydatabase (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
VALUES ('$first_name','$last_name','$phone_number','$address','city','state','zip','$country','$email_address', '$comments','$join_mailing_list')";

So when line 21 is reached $mysqli does not exist yet! hence undefined error.

Line 29 comment states

// define variables and set to empty values

BUT! each variable from line 30 to 40 is not set to empty string, it set to reference a $_POST['....']' named index;, which on loading of this page do not exist either. Only when the form is submitted are these available. That is why line 49 to 135, check if $_POST['...'] name indexes exist and it also carries a value.

BUT! these have been set to empty values at line 43 so you don't need lines 30 to 40, I suggest you comment these out.

The last error line 205 is because

mysqli_close($connect);

Has already been called on line 27, in other words the connection is already closed and does not exist anymore.

Remember, any variable referred to, must have been defined beforehand.

Example: line 46 of line 45 to 46 will be a problem, because all these variables are updated by the coding below it (line 49 to 135). In its present position they will all be empty, because they directly follow the setting of these variables to a empty string line 43.

 

Share this post


Link to post
Share on other sites

HI OK.  Thank you

I rearranged a few things and that got rid of all the error messages... BUT!~

  • Even though the records are being created in the database,  they are still empty.  I'm guessing that the records are being created empty because somehow I am still emptying them after the info has been sent.. I just don't see how when I look at my code. hint?
  • The emails are being sent to me but they are also empty. am guessing if I fix the above issue, this will resolve itself
  • The emails are NOT being sent to the user as I instructed the code to do. hmm....I have scoured the code and not been able to see my error. I believe I set it up to draw the user's email from the html form info he enters above, and to automatically send the user a copy.  I copied the same bit of code for $usersuccess from right above it $success (where it successfully sends me the email) and just changed the $variables. ideas?
  • It still says "record created successfully all the time. not only when the form has been sent.   Do I need to put in something like this on line # 195? -  $failed= if (!$connect) {
        die("Connection failed: " . mysqli_connect_error());
    }   if (empty($mysqli) {$failed) { $failederrormessage = "An attempt to send your message has failed. Please try again."; } else {  $success, $usersuccess

Here is the new layout of the code: 

Thanks again for your help!

1 <?php
2 // define the conection info
3 $host_name  = "...........";
4 $database   = "...........";
5 $user_name  = "...........";
6 $password   = "...........";
7
8 // define the connection and then connect
9 $connect = mysqli_connect($host_name, $user_name, $password, $database);
10
11 //keep this in here to display all the errors on debugging
12 ini_set('display_errors', 1);
13 	error_reporting(E_ALL);
14 //this is where the debugging portion ends
15 
16 // set all the fields on the new database record to empty initially to reset it from the previous user (do I need to autoincrement here somewhere? Maybe my database settings are wrong?)
17 $first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";
18 $first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $join_mailing_list = "";
19
20 ?>
21 <!-- the form itself . How to put this in the php code? reference my attempt below the form code-->
22 <p><span class="error">* required field.</span></p>
23 <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="text/plain">
24        First Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="First Name" required name="first_name" value="<?php echo $first_name;?>"><span class="error">* <?php echo $first_nameErr;?></span><br>
25        Last Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Last Name" required name="last_name" value="<?php echo $last_name;?>"><span class="error">* <?php echo $last_nameErr;?></span><br>
26        Phone:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Phone" required name="phone_number" size="50" value="<?php echo $phone_number;?>"><span class="error">* <?php echo $phone_numberErr;?></span><br>
 27       Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Address" required name="address" value="<?php echo $address;?>"><span class="error">* <?php echo $addressErr;?></span><br>
28        City:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="City" required name="city" value="<?php echo $city;?>"><span class="error">* <?php echo $cityErr;?></span><br>
29        State:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="State" required name="state" value="<?php echo $state;?>"><span class="error">* <?php echo $stateErr;?></span><br>
30        Zip Code:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Zip" required name="zip" value="<?php echo $zip;?>"><span class="error">* <?php echo $zipErr;?></span><br>
31        Country:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Country" required name="country" value="<?php echo $country;?>"><span class="error">* <?php echo $countryErr;?></span><br>
32       Email Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Email Address" required name="email_address" value="<?php echo $email_address;?>"><span class="error">* <?php echo $email_addressErr;?></span><br>
33       Comment:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Message" required name="comments" size="75" value="<?php echo $comments;?>"><span class="error">* <?php echo $commentsErr;?></span><br>
34        Would you like to be on our mailing list?<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list=="yes") echo "checked";?>
35 value="yes"><label>Yes</label>
36		<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list=="no") echo "checked";?>
37 value="no"><label>No</label><span class="error">* <?php echo $join_mailing_listErr;?></span>
38 <br>
39        <input class="w3-input w3-padding-16 w3-border" type="datetime-local" placeholder="Date and time" required name="date" value="2017-11-16T20:00"><br>
40        <button class="w3-button w3-black"><input type="submit" name="submit" value="Submit"></button>
41      </form>
42
43
44 <?php
45 // check the connection
46 if (!$connect) {
47    die("Connection failed: " . mysqli_connect_error());
48 }
49
50 // define what $mysqli is made up of (taken from the fields in the html form code above)
51 $mysqli = "INSERT INTO mydatabasename (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
52 VALUES ('$first_name','$last_name','$phone_number','$address','$city','$state','$zip','$country','$email_address', '$comments','$join_mailing_list')";
53
54
55 // if connected this says to give a success message and enter the info from $mysqli which was defined above
56 if (mysqli_query($connect, $mysqli)) {
57    echo "New record created successfully";
58 } else {
59    echo "Error: " . $mysqli . "<br>" . mysqli_error($connect);
60 }
61
62 //This tells the server what to do with the user's info (I suspect this should be after the html form but then the other portion of defining mysqli will have to be moved down further as well)
63
64 if ($_SERVER["REQUEST_METHOD"] == "POST") { // if connected to the server and it requests info then post the following info
65  if (empty($_POST["first_name"])) {  // this checks to see if the fiedl on teh users browser website page is empty or if they filled it in
66    $first_nameErr = "First name is required"; // if the field on the users computer is left empty when posting, this tells gives the user an error message
67  } else {  
68    $first_name = test_input($_POST["first_name"]); // if the fiedl has been filled in, then run it through the following test to test it for hacking characters
69  if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {  // if there is a match of any of the hacking characters listed here, then send an error message to the user
70    $first_nameErr = "Only letters and white space allowed"; // this is the error message sent if there is a strange character in the field
71 }
72 }
73  if (empty($_POST["last_name"])) {
74    $last_nameErr = "Last name is required";
75  } else {
76    $last_name = test_input($_POST["last_name"]);
77  if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
78    $last_nameErr = "Only letters and white space allowed"; 
79 }
80 }
81  if (empty($_POST["phone_number"])) {
82    $phone_numberErr = "Please enter a phone number";
83  } else {
84    $phone_number = test_input($_POST["phone_number"]);
85 }
86  if (empty($_POST["address"])) {
87    $addressErr = "Address is required";
88  } else {
89    $address = test_input($_POST["address"]);
90 }
91
92 if (empty($_POST["city"])) {
93   $cityErr = "City is required";
94  } else {
95    $city = test_input($_POST["city"]);
96	if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
97    $cityErr = "Only letters and white space allowed"; 
98 }
99 }
100 if (empty($_POST["state"])) {
101     $stateErr = "State is required";
102  } else {
103    $state = test_input($_POST["state"]);
104	if (!preg_match("/^[a-zA-Z ]*$/",$state)) {
105    $stateErr = "Only letters and white space allowed"; 
106 }
107 }
108
109 if (empty($_POST["zip"])) {
110    $zipErr = "Zip is required";
111  } else {
112    $zip = test_input($_POST["zip"]);
113 }
114
115 if (empty($_POST["country"])) {
116    $countryERR = "please enter your country";
117   } else {
118   $country = test_input($_POST["country"]);
119   if (!preg_match("/^[a-zA-Z ]*$/",$country)) {
120    $countryErr = "Only letters and white space allowed"; 
121 }
122 }
123
124  if (empty($_POST["email_address"])) {
125    $email_addressErr = "Email is required";
126  } else {
127    $email_address = test_input($_POST["email_address"]);
128  if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
129    $email_addressErr = "Invalid email format";
130 }
131 }
132 
133  if (empty($_POST["comments"])) {
134    $comments = "";
135  } else {
136    $comments = test_input($_POST["comments"]);
137 }
138  if (empty($_POST["join_mailing_list"])) {
139    $join_mailing_listErr = "requires a yes or no";
140  } else {
141    $join_mailing_list = test_input($_POST["join_mailing_list"]);
142 }
143	
144 // define variables as per what the user has entered on the page I thought, but am told I don't need these so will try to go without. :) 
145 // $first_name = $_POST['first_name'];
146 // $last_name = $_POST['last_name'];
147 // $phone_number = $_POST['phone_number'];
148 // $address = $_POST['address'];
149 // $city = $_POST['city'];
150 // $state = $_POST['state'];
151 // $zip = $_POST['zip'];
152 // $email_address = $_POST['email_address'];
153 // $country = $_POST['country'];
154 // $comments = $_POST['comments'];
155 // $join_mailing_list = $_POST['join_mailing_list'];
156 // $name=$_POST['name'];
157
158	
159 // test again and strip the user info of characters I don't want
160 function test_input($data) {
161  $data = trim($data);
162  $data = stripslashes($data);
163  $data = htmlspecialchars($data);
164  return $data;
165 }
166 }
167 // define what $email, and $email Subject is made up of
168 $email = "myemail@gmail.com";
169 $emailSubject = "Inquiry from the Contact Page";
170
171 // define what the headers should say
172 $headers = "From:$email_address\r\n";
173 $headers = "Content-type: text/html\r\n";
174
175 // define what teh body of the email should say and look like
176 $body = <<<EOD
177 <br><hr><br>
178 First Name : $first_name <br>
179 Last Name : $last_name <br>
180 Phone Number : $phone_number <br>
181 Address : $address <br>
182 City : $city <br>
183 State : $state <br>
184 Zip : $zip <br>
185 Country : $country <br>
186 Email Address : $email_address <br>
187 Comments : $comments <br>
188 Subscribe to Mailing List : $join_mailing_list <br>
189 EOD;
190
191 // define what to upon success of a new record having been created - mail it with the email info, email subject, body, and headers to my email address
192 $success = mail($email, $emailSubject, $body, $headers);
193 // define what to upon success of a new record having been created - mail it to the user's email info (which was defined above in the form), email subject, body, and headers
194 $usersuccess = mail($email_address, $emailSubject, $body, $headers);
195
196 // on this same users page on his computer in the browser window, next,  print on screen a copy of what they entered above
197 echo "<h2>Thank you your message was received successfully. Here is a copy of what you sent us:</h2>";
198 echo $first_name;
199 echo "<br>";
200 echo $last_name;
201 echo "<br>";
202 echo $phone_number;
203 echo "<br>";
204 echo $address;
205 echo "<br>";
206 echo $city;
207 echo "<br>";
208 echo $state;
209 echo "<br>";
210 echo $zip;
211 echo "<br>";
212 echo $country;
213 echo "<br>";
214 echo $email_address;
215 echo "<br>";
216 echo $comments;
217 echo "<br>";
218 echo $join_mailing_list;
219 
220 // when everything has been done, close the database connection
221 mysqli_close($connect);
222 
223 ?>   

 

Edited by emcy

Share this post


Link to post
Share on other sites

The database only needs to connected to when the form is submitted, and all the valid code is present. Ideally i would setup a $passed=true; variable after  if ($_SERVER["REQUEST_METHOD"] == "POST") then if any errors are discovered ( where error message if produced) set to $passed = false;

Use this variable to check if all form data passed so $passed remains true with if condition. The add database connection, creating of email, anything related for completion within that if condition below line 65 after the function.

The form needs to below the setting of empty string value and the validation of submitted data from form to show error messages.

This form will appear along side the display of data set through email, so you might to check if email fail show form again, else show the other.

Line 16 has nothing to with setting database field to zero, these hold values that should hold some value otherthan "", before getting anywhere near being added to database.

Auto incremental fields dont need to be included, as name suggests it autoincrements on every new record created.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

If the request method is post, then that generally means a form has been submitted.  The request method is usually either get, for most normal web page requests, or post, where the browser is sending data to the server, like form data.

Share this post


Link to post
Share on other sites

um..... I must be getting close.. it's more confusing LOL!

I have commented by the various parts in the code, and labelled the different sections to make it easier to reference...  I don't have to keep adding the line numbers this way when I am talking with you.

Thanks for you response. I am not sure how to interpret what you wrote..  did you mean that I should re-write the code piece like this?....

   if ($_SERVER["REQUEST_METHOD"] == "POST") {    // if connected to the server and it requests info then post the following info
$passed=true;
 } else { $passed = false;   }

* The form needs to below the setting of empty string value,  and below the validation of submitted data from form to show error messages. Ok. I moved it.. BUT..... in my head there is a connection missing then.... maybe it's just a theorem I need to accept? - i.e. if php reads from the top down, and the form (SECTION D) is positioned after the 'validation of the submitted data from the form'... then how can it validate the information from the submitted data if the form comes after it?  That says to me that it doesn't have a form to validate but thinks it does and carries out the action.. but how?.. huh? I'm  confused on this part.

* This form will appear along side the display of data se(n)t through email, so you might (want)to check if email fail(s to) show form again, else show the other (other what?). Huh?  I didn't understand your sentence here.....

Line 16 has nothing to with setting database field to zero, these hold values that should hold some value otherthan "", before getting anywhere near being added to database.

Auto incremental fields don't need to be included, as name suggests it autoincrements on every new record created. OK

Ok it now looks like this:
I put that little piece of code in there... then moved the form (SECTION D)  to go after the connection is made/tested and has cleared the variables. Then I put the instructions on what to with the form data after the form submit button.

To me ... this makes sense...but it's not working and I am now getting an error message such as this..

Parse error: syntax error, unexpected end of file in /homepages/42//htdocs/contactcode.php on line 238

Line 238 is mysqli_close($connect);

 

 

<?php
//SECTION A
// define the conection info
// got this info from 1and1.com directly
$host_name = ".........";
$user_name = ".........";
$password = ".........";
$database = ".........";

// define the connection and then connect
$connect = mysqli_connect($host_name, $user_name, $password, $database);

//keep this in here to display all the errors on debugging
ini_set('display_errors', 1);
error_reporting(E_ALL);
//this is where the debugging portion ends

// set all the fields on the new database record to empty initially to reset it from the previous user (do I need to autoincrement here somewhere? Maybe my database settings are wrong?)
$first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";
$first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $join_mailing_list = "";

// SECTION B
// check the connection
if (!$connect) {
    die("Connection failed: " . mysqli_connect_error());
 }

// SECTION C
 //This tells the server what to do with the user's info when it gets to the SECTION D of this php page (I suspect this should be after the  SECTION D but then SECTION E of defining mysqli will have to be moved down further as well)

 if ($_SERVER["REQUEST_METHOD"] == "POST") { // if connected to the server successfully and it requests info or is ready to receive the info....
$passed=true;
 } else { $passed = false; }
    
// then start working on the user's info to clean it up and make sure all the fields have been filled in
  if (empty($_POST["first_name"])) {  // this checks to see if the field on the users browser website page is empty or if they filled it in
    $first_nameErr = "First name is required"; // if the field on the users computer is left empty when posting, this tells gives the user an error message
  } else {  
    $first_name = test_input($_POST["first_name"]); // if the field has been filled in, then run it through the following test to test it for hacking characters
  if (!preg_match("/^[a-zA-Z ]*$/",$first_name)) {  // if there is a match of any of the hacking characters listed here, then send an error message to the user
    $first_nameErr = "Only letters and white space allowed"; // this is the error message sent if there is a strange character in the field
 }
 }
  if (empty($_POST["last_name"])) {
    $last_nameErr = "Last name is required";
  } else {
    $last_name = test_input($_POST["last_name"]);
  if (!preg_match("/^[a-zA-Z ]*$/",$last_name)) {
    $last_nameErr = "Only letters and white space allowed";
 }
 }
  if (empty($_POST["phone_number"])) {
    $phone_numberErr = "Please enter a phone number";
  } else {
    $phone_number = test_input($_POST["phone_number"]);
 }
  if (empty($_POST["address"])) {
    $addressErr = "Address is required";
  } else {
    $address = test_input($_POST["address"]);
 }

 if (empty($_POST["city"])) {
   $cityErr = "City is required";
  } else {
    $city = test_input($_POST["city"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
    $cityErr = "Only letters and white space allowed";
 }
 }
 if (empty($_POST["state"])) {
     $stateErr = "State is required";
  } else {
    $state = test_input($_POST["state"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$state)) {
    $stateErr = "Only letters and white space allowed";
 }
 }

 if (empty($_POST["zip"])) {
    $zipErr = "Zip is required";
  } else {
    $zip = test_input($_POST["zip"]);
 }

 if (empty($_POST["country"])) {
    $countryERR = "please enter your country";
   } else {
   $country = test_input($_POST["country"]);
   if (!preg_match("/^[a-zA-Z ]*$/",$country)) {
    $countryErr = "Only letters and white space allowed";
 }
 }

  if (empty($_POST["email_address"])) {
    $email_addressErr = "Email is required";
  } else {
    $email_address = test_input($_POST["email_address"]);
  if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
    $email_addressErr = "Invalid email format";
 }
 }

  if (empty($_POST["comments"])) {
    $comments = "";
  } else {
   $comments = test_input($_POST["comments"]);
 }
  if (empty($_POST["join_mailing_list"])) {
    $join_mailing_listErr = "requires a yes or no";
  } else {
    $join_mailing_list = test_input($_POST["join_mailing_list"]);
 }
    
 // define variables as per what the user has entered on the page I thought, but am told I don't need these so will try to go without. :)
 // $first_name = $_POST['first_name'];
 // $last_name = $_POST['last_name'];
 // $phone_number = $_POST['phone_number'];
 // $address = $_POST['address'];
 // $city = $_POST['city'];
 // $state = $_POST['state'];
 // $zip = $_POST['zip'];
 // $email_address = $_POST['email_address'];
 // $country = $_POST['country'];
 // $comments = $_POST['comments'];
 // $join_mailing_list = $_POST['join_mailing_list'];
 // $name=$_POST['name'];

 // test again and strip the user info of characters I don't want
 function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
 }
?>

<!-- SECTION D - Here is where the user enterss the info for the database to  it up -->
<!-- Middle Column section -->
    <!-- the form itself -->
<p><span class="error">* required field.</span></p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="text/plain">
       First Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="First Name" required name="first_name" value="<?php echo $first_name;?>"><span class="error">* <?php echo $first_nameErr;?></span><br>
       Last Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Last Name" required name="last_name" value="<?php echo $last_name;?>"><span class="error">* <?php echo $last_nameErr;?></span><br>
        Phone:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Phone" required name="phone_number" size="50" value="<?php echo $phone_number;?>"><span class="error">* <?php echo $phone_numberErr;?></span><br>
       Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Address" required name="address" value="<?php echo $address;?>"><span class="error">* <?php echo $addressErr;?></span><br>
        City:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="City" required name="city" value="<?php echo $city;?>"><span class="error">* <?php echo $cityErr;?></span><br>
       State:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="State" required name="state" value="<?php echo $state;?>"><span class="error">* <?php echo $stateErr;?></span><br>
       Zip Code:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Zip" required name="zip" value="<?php echo $zip;?>"><span class="error">* <?php echo $zipErr;?></span><br>
       Country:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Country" required name="country" value="<?php echo $country;?>"><span class="error">* <?php echo $countryErr;?></span><br>
      Email Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Email Address" required name="email_address" value="<?php echo $email_address;?>"><span class="error">* <?php echo $email_addressErr;?></span><br>
      Comment:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Message" required name="comments" size="75" value="<?php echo $comments;?>"><span class="error">* <?php echo $commentsErr;?></span><br>
       Would you like to be on our mailing list?<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list=="yes") echo "checked";?>
value="yes"><label>Yes</label>
        <input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list=="no") echo "checked";?>
 value="no"><label>No</label><span class="error">* <?php echo $join_mailing_listErr;?></span>
 <br>
        <input class="w3-input w3-padding-16 w3-border" type="datetime-local" placeholder="Date and time" required name="date" value="2017-11-16T20:00"><br>
        <button class="w3-button w3-black"><input type="submit" name="submit" value="Submit"></button>
     </form>

<?php
//SECTION E
 // define what $mysqli is made up of (taken from the fields in the html form code above)
 $mysqli = "INSERT INTO mydatabase (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
 VALUES ('$first_name','$last_name','$phone_number','$address','$city','$state','$zip','$country','$email_address', '$comments','$join_mailing_list')";

//SECTION F
 // Checking the connection and verifying the user's information/$mysqli (which was defined above) has been entered into the new record in the database, then it gives a success message 
 if (mysqli_query($connect, $mysqli)) {
    echo "New record created successfully";
 } else {
    echo "Error: " . $mysqli . "<br>" . mysqli_error($connect);
 }


// SECTION G
 // define what $email, and $email Subject is made up of for when it sends an email to me to let me know someone just posted something.
 $email = "myemail@gmail.com";
 $emailSubject = "Inquiry from the Contact Page";

 // define what the headers of the email should say
 $headers = "From:$email_address\r\n";
 $headers = "Content-type: text/html\r\n";

 // define what the body of the email should say and look like
 $body = <<<EOD
 <br><hr><br>
 First Name : $first_name <br>
 Last Name : $last_name <br>
 Phone Number : $phone_number <br>
 Address : $address <br>
 City : $city <br>
 State : $state <br>
 Zip : $zip <br>
 Country : $country <br>
 Email Address : $email_address <br>
 Comments : $comments <br>
 Subscribe to Mailing List : $join_mailing_list <br>
 EOD;

// SECTION H
 // define the action to take upon success of a new record having been created - i.e. mail it with the email info, email subject, body, and headers from the above definitions, to my email address
 $success = mail($email, $emailSubject, $body, $headers);
 // define the action to take upon success of a new record having been created - i.e. mail it with the email info, email subject, body, and headers from the above definitions, to the user's email address
 $usersuccess = mail($email_address, $emailSubject, $body, $headers);

// SECTION I
 // on this same user's page on his computer in the browser window, next, print on screen a copy of what the user entered above
 echo "<h2>Thank you your message was received successfully. Here is a copy of what you sent us:</h2>";
 echo $first_name;
 echo "<br>";
 echo $last_name;
 echo "<br>";
 echo $phone_number;
 echo "<br>";
 echo $address;
 echo "<br>";
 echo $city;
 echo "<br>";
 echo $state;
 echo "<br>";
 echo $zip;
 echo "<br>";
 echo $country;
 echo "<br>";
 echo $email_address;
 echo "<br>";
 echo $comments;
 echo "<br>";
 echo $join_mailing_list;
 
// SECTION J
 // when everything has been done, close the database connection
 // NOTE I am getting a red error note on the Dreamweaver software panel saying there is something wrong with this being here.. why?!?!?!?!
    mysqli_close($connect);
    
 ?>   
    </body>
</html>

 

Edited by emcy

Share this post


Link to post
Share on other sites

Hi JustSomeguy. Thanks! If I understand it correctly...

"If the request method is post, then that generally means a form has been submitted" - so this should come after the form. yes?

Edited by emcy

Share this post


Link to post
Share on other sites

That parse error probably means you have an opening bracket without a matching closing bracket.  When it says unexpected end of file that's usually what it means.

so this should come after the form. yes?

You should structure your PHP code so that any processing like form processing should be at the top.  All of your logic should be at the start of the file, because you may want to redirect, or print HTML, or take some other action depending on the data that PHP got.  So all of your form handling/processing code should be at the top of the file, and you process the form if it was submitted, gather any error messages, decide whatever other actions you want to do (send email, redirect, etc), and then decide if you want to show the form and any error messages.  In the code above you are doing everything every time.  Every time that file gets loaded you try to process form data, even if none was submitted, then you try to insert a database record, send an email, and print a confirmation.  You do all of that regardless of whether the form was submitted.  You should do all of the form handling/email/database stuff at the start of the file, before you print any HTML (because based on the data, you might not want to print HTML).  Then when you're deciding what HTML to print you can check your other variables to see if you should print the confirmation message, or the form with error messages, or whatever else.

Share this post


Link to post
Share on other sites

The unexpected end of file could also be because of that heredoc you use to define to email body.  The closing tag for the heredoc needs to start on the line, if you have any spaces or any other characters before it then it does not end the text.

Share this post


Link to post
Share on other sites

The probable reason it throwing an error is because the heredoc section closing EOD must NOT be indented or have anything before it, else the following code after it is treated as text, meaning php code may not be closing opening properly and become indistinguishable from php, comments or html.

IT should be similar to this

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
        <script type="text/javascript">

        </script>
        <style type="text/css">

        </style>
    </head>
    <body>
        <?php
//keep this in here to display all the errors on debugging
        ini_set('display_errors', 1);
        error_reporting(E_ALL);

//SECTION A
// define the conection info
// got this info from 1and1.com directly
        $host_name = ".........";
        $user_name = ".........";
        $password = ".........";
        $database = ".........";

// define the connection and then connect
        $connect = mysqli_connect($host_name, $user_name, $password, $database);


//this is where the debugging portion ends
// set all the fields on the new database record to empty initially to reset it from the previous user (do I need to autoincrement here somewhere? Maybe my database settings are wrong?)
        $first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";
        $first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $join_mailing_list = "";

// SECTION B
// check the connection
        if (!$connect) {
            die("Connection failed: " . mysqli_connect_error());
        }

// SECTION C
        //This tells the server what to do with the user's info when it gets to the SECTION D of this php page (I suspect this should be after the  SECTION D but then SECTION E of defining mysqli will have to be moved down further as well)

        if ($_SERVER["REQUEST_METHOD"] == "POST") { // if connected to the server successfully and it requests info or is ready to receive the info....
            $passed = true;
            //} else { $passed = false; }
// then start working on the user's info to clean it up and make sure all the fields have been filled in
            if (empty($_POST["first_name"])) {  // this checks to see if the field on the users browser website page is empty or if they filled it in
                $passed = false;
                $first_nameErr = "First name is required"; // if the field on the users computer is left empty when posting, this tells gives the user an error message
            } else {
                $first_name = test_input($_POST["first_name"]); // if the field has been filled in, then run it through the following test to test it for hacking characters
                if (!preg_match("/^[a-zA-Z ]*$/", $first_name)) {  // if there is a match of any of the hacking characters listed here, then send an error message to the user
                    $passed = false;
                    $first_nameErr = "Only letters and white space allowed"; // this is the error message sent if there is a strange character in the field
                }
            }
            if (empty($_POST["last_name"])) {
                $last_nameErr = "Last name is required";
                $passed = false;
            } else {
                $last_name = test_input($_POST["last_name"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $last_name)) {
                    $passed = false;
                    $last_nameErr = "Only letters and white space allowed";
                }
            }
            if (empty($_POST["phone_number"])) {
                $phone_numberErr = "Please enter a phone number";
                $passed = false;
            } else {
                $phone_number = test_input($_POST["phone_number"]);
            }
            if (empty($_POST["address"])) {
                $addressErr = "Address is required";
                $passed = false;
            } else {
                $address = test_input($_POST["address"]);
            }

            if (empty($_POST["city"])) {
                $cityErr = "City is required";
                $passed = false;
            } else {
                $city = test_input($_POST["city"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $city)) {
                    $passed = false;
                    $cityErr = "Only letters and white space allowed";
                }
            }
            if (empty($_POST["state"])) {
                $stateErr = "State is required";
                $passed = false;
            } else {
                $state = test_input($_POST["state"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $state)) {
                    $stateErr = "Only letters and white space allowed";
                    $passed = false;
                }
            }

            if (empty($_POST["zip"])) {
                $zipErr = "Zip is required";
                $passed = false;
            } else {
                $zip = test_input($_POST["zip"]);
            }

            if (empty($_POST["country"])) {
                $countryERR = "please enter your country";
                $passed = false;
            } else {
                $country = test_input($_POST["country"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $country)) {
                    $passed = false;
                    $countryErr = "Only letters and white space allowed";
                }
            }

            if (empty($_POST["email_address"])) {
                $email_addressErr = "Email is required";
                $passed = false;
            } else {
                $email_address = test_input($_POST["email_address"]);
                if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
                    $passed = false;
                    $email_addressErr = "Invalid email format";
                }
            }

            if (empty($_POST["comments"])) {
                $comments = "";
            } else {
                $comments = test_input($_POST["comments"]);
            }
            if (empty($_POST["join_mailing_list"])) {
                $join_mailing_listErr = "requires a yes or no";
                $passed = false;
            } else {
                $join_mailing_list = test_input($_POST["join_mailing_list"]);
            }
        }

        // define variables as per what the user has entered on the page I thought, but am told I don't need these so will try to go without. :)
        // $first_name = $_POST['first_name'];
        // $last_name = $_POST['last_name'];
        // $phone_number = $_POST['phone_number'];
        // $address = $_POST['address'];
        // $city = $_POST['city'];
        // $state = $_POST['state'];
        // $zip = $_POST['zip'];
        // $email_address = $_POST['email_address'];
        // $country = $_POST['country'];
        // $comments = $_POST['comments'];
        // $join_mailing_list = $_POST['join_mailing_list'];
        // $name=$_POST['name'];
        // test again and strip the user info of characters I don't want
        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }
        ?>

        <!-- SECTION D - Here is where the user enterss the info for the database to  it up -->
        <!-- Middle Column section -->
        <!-- the form itself -->
        <p><span class="error">* required field.</span></p>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="text/plain">
            First Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="First Name" required name="first_name" value="<?php echo $first_name; ?>"><span class="error">* <?php echo $first_nameErr; ?></span><br>
            Last Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Last Name" required name="last_name" value="<?php echo $last_name; ?>"><span class="error">* <?php echo $last_nameErr; ?></span><br>
            Phone:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Phone" required name="phone_number" size="50" value="<?php echo $phone_number; ?>"><span class="error">* <?php echo $phone_numberErr; ?></span><br>
            Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Address" required name="address" value="<?php echo $address; ?>"><span class="error">* <?php echo $addressErr; ?></span><br>
            City:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="City" required name="city" value="<?php echo $city; ?>"><span class="error">* <?php echo $cityErr; ?></span><br>
            State:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="State" required name="state" value="<?php echo $state; ?>"><span class="error">* <?php echo $stateErr; ?></span><br>
            Zip Code:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Zip" required name="zip" value="<?php echo $zip; ?>"><span class="error">* <?php echo $zipErr; ?></span><br>
            Country:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Country" required name="country" value="<?php echo $country; ?>"><span class="error">* <?php echo $countryErr; ?></span><br>
            Email Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Email Address" required name="email_address" value="<?php echo $email_address; ?>"><span class="error">* <?php echo $email_addressErr; ?></span><br>
            Comment:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Message" required name="comments" size="75" value="<?php echo $comments; ?>"><span class="error">* <?php echo $commentsErr; ?></span><br>
            Would you like to be on our mailing list?<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list == "yes") echo "checked"; ?>
                                                            value="yes"><label>Yes</label>
            <input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list == "no") echo "checked"; ?>
                   value="no"><label>No</label><span class="error">* <?php echo $join_mailing_listErr; ?></span>
            <br>
            <input class="w3-input w3-padding-16 w3-border" type="datetime-local" placeholder="Date and time" required name="date" value="2017-11-16T20:00">
            <!--- PLACEHOLDER Not allowed on this type ABOVE-->
            <br>
            <button class="w3-button w3-black"><input type="submit" name="submit" value="Submit"></button>
        </form>

        <?php
        if ($passed) {


//SECTION E
// define what $mysqli is made up of (taken from the fields in the html form code above)
            $mysqli = "INSERT INTO mydatabase (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
 VALUES ('$first_name','$last_name','$phone_number','$address','$city','$state','$zip','$country','$email_address', '$comments','$join_mailing_list')";

//SECTION F
// Checking the connection and verifying the user's information/$mysqli (which was defined above) has been entered into the new record in the database, then it gives a success message
            if (mysqli_query($connect, $mysqli)) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $mysqli . "<br>" . mysqli_error($connect);
            }


// SECTION G
// define what $email, and $email Subject is made up of for when it sends an email to me to let me know someone just posted something.
            $email = "myemail@gmail.com";
            $emailSubject = "Inquiry from the Contact Page";

// define what the headers of the email should say
            $headers = "From:$email_address\r\n";
            $headers = "Content-type: text/html\r\n";

// define what the body of the email should say and look like
            $body = <<<EOD
 <br><hr><br>
 First Name : $first_name <br>
 Last Name : $last_name <br>
 Phone Number : $phone_number <br>
 Address : $address <br>
 City : $city <br>
 State : $state <br>
 Zip : $zip <br>
 Country : $country <br>
 Email Address : $email_address <br>
 Comments : $comments <br>
 Subscribe to Mailing List : $join_mailing_list <br>
EOD;

// SECTION H
            // define the action to take upon success of a new record having been created - i.e. mail it with the email info, email subject, body, and headers from the above definitions, to my email address
            $success = mail($email, $emailSubject, $body, $headers);
            // define the action to take upon success of a new record having been created - i.e. mail it with the email info, email subject, body, and headers from the above definitions, to the user's email address
            $usersuccess = mail($email_address, $emailSubject, $body, $headers);

// SECTION I
            // on this same user's page on his computer in the browser window, next, print on screen a copy of what the user entered above
            echo "<h2>Thank you your message was received successfully. Here is a copy of what you sent us:</h2>";
            echo $first_name;
            echo "<br>";
            echo $last_name;
            echo "<br>";
            echo $phone_number;
            echo "<br>";
            echo $address;
            echo "<br>";
            echo $city;
            echo "<br>";
            echo $state;
            echo "<br>";
            echo $zip;
            echo "<br>";
            echo $country;
            echo "<br>";
            echo $email_address;
            echo "<br>";
            echo $comments;
            echo "<br>";
            echo $join_mailing_list;

// SECTION J
            // when everything has been done, close the database connection
            // NOTE I am getting a red error note on the Dreamweaver software panel saying there is something wrong with this being here.. why?!?!?!?!
        }
        mysqli_close($connect);
        ?>
    </body>
</html>

Everything must validate and not produce an error, else passed=false; will also be produced which will overwrite passed=true; at the beginning following

if ($_SERVER["REQUEST_METHOD"] == "POST") {

meaning it won't get pass

	if ($passed) {
	

meaning the process of inserting into database and sending email won't take place until $passed = true and remains true after passing the validation coding.

Edited by dsonesuk

Share this post


Link to post
Share on other sites

The reason the input are not being saved to database is the encode type used enctype="text/plain" instead of enctype="application/x-www-form-urlencoded"

final

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" id="viewport" content="target-densitydpi=high-dpi,initial-scale=1.0,user-scalable=no" />
        <title>Document Title</title>
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <script  type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
        <script type="text/javascript">

        </script>
        <style type="text/css">

        </style>
    </head>
    <body>
        <?php
//keep this in here to display all the errors on debugging
        ini_set('display_errors', 1);
        error_reporting(E_ALL);

//SECTION A
// define the conection info
// got this info from 1and1.com directly
        $host_name = "........";
        $user_name = "........";
        $password = "........";
        $database = "........";

// define the connection and then connect
        $connect = mysqli_connect($host_name, $user_name, $password, $database);


//this is where the debugging portion ends
// set all the fields on the new database record to empty initially to reset it from the previous user (do I need to autoincrement here somewhere? Maybe my database settings are wrong?)
// SECTION B
// check the connection
        if (!$connect) {
            die("Connection failed: " . mysqli_connect_error());
        }

// SECTION C
        //This tells the server what to do with the user's info when it gets to the SECTION D of this php page (I suspect this should be after the  SECTION D but then SECTION E of defining mysqli will have to be moved down further as well)
        // test again and strip the user info of characters I don't want
        function test_input($data) {
            $data = trim($data);
            $data = stripslashes($data);
            $data = htmlspecialchars($data);
            return $data;
        }

        $first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $join_mailing_list = "";
        $first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";

        if ($_SERVER["REQUEST_METHOD"] == "POST") { // if connected to the server successfully and it requests info or is ready to receive the info....
            $passed = true;


            //} else { $passed = false; }
// then start working on the user's info to clean it up and make sure all the fields have been filled in
            if (empty($_POST["first_name"])) {  // this checks to see if the field on the users browser website page is empty or if they filled it in
                $passed = false;
                $first_nameErr = "First name is required"; // if the field on the users computer is left empty when posting, this tells gives the user an error message
            } else {
                $first_name = test_input($_POST["first_name"]); // if the field has been filled in, then run it through the following test to test it for hacking characters
                if (!preg_match("/^[a-zA-Z ]*$/", $first_name)) {  // if there is a match of any of the hacking characters listed here, then send an error message to the user
                    $passed = false;
                    $first_nameErr = "Only letters and white space allowed"; // this is the error message sent if there is a strange character in the field
                }
            }
            if (empty($_POST["last_name"])) {
                $last_nameErr = "Last name is required";
                $passed = false;
            } else {
                $last_name = test_input($_POST["last_name"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $last_name)) {
                    $passed = false;
                    $last_nameErr = "Only letters and white space allowed";
                }
            }
            if (empty($_POST["phone_number"])) {
                $phone_numberErr = "Please enter a phone number";
                $passed = false;
            } else {
                $phone_number = test_input($_POST["phone_number"]);
            }
            if (empty($_POST["address"])) {
                $addressErr = "Address is required";
                $passed = false;
            } else {
                $address = test_input($_POST["address"]);
            }

            if (empty($_POST["city"])) {
                $cityErr = "City is required";
                $passed = false;
            } else {
                $city = test_input($_POST["city"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $city)) {
                    $passed = false;
                    $cityErr = "Only letters and white space allowed";
                }
            }
            if (empty($_POST["state"])) {
                $stateErr = "State is required";
                $passed = false;
            } else {
                $state = test_input($_POST["state"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $state)) {
                    $stateErr = "Only letters and white space allowed";
                    $passed = false;
                }
            }

            if (empty($_POST["zip"])) {
                $zipErr = "Zip is required";
                $passed = false;
            } else {
                $zip = test_input($_POST["zip"]);
            }

            if (empty($_POST["country"])) {
                $countryERR = "please enter your country";
                $passed = false;
            } else {
                $country = test_input($_POST["country"]);
                if (!preg_match("/^[a-zA-Z ]*$/", $country)) {
                    $passed = false;
                    $countryErr = "Only letters and white space allowed";
                }
            }

            if (empty($_POST["email_address"])) {
                $email_addressErr = "Email is required";
                $passed = false;
            } else {
                $email_address = test_input($_POST["email_address"]);
                if (!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
                    $passed = false;
                    $email_addressErr = "Invalid email format";
                }
            }

            if (empty($_POST["comments"])) {
                $commentsErr = "Comments is required";
                $passed = false;
            } else {
                $comments = test_input($_POST["comments"]);
            }

            if (empty($_POST["join_mailing_list"])) {
                $join_mailing_listErr = "requires a yes or no";
                $passed = false;
            } else {
                $join_mailing_list = test_input($_POST["join_mailing_list"]);
            }


            // define variables as per what the user has entered on the page I thought, but am told I don't need these so will try to go without. :)
            // $first_name = $_POST['first_name'];
            // $last_name = $_POST['last_name'];
            // $phone_number = $_POST['phone_number'];
            // $address = $_POST['address'];
            // $city = $_POST['city'];
            // $state = $_POST['state'];
            // $zip = $_POST['zip'];
            // $email_address = $_POST['email_address'];
            // $country = $_POST['country'];
            // $comments = $_POST['comments'];
            // $join_mailing_list = $_POST['join_mailing_list'];
            // $name=$_POST['name'];
            ?>

            <!-- SECTION D - Here is where the user enterss the info for the database to  it up -->
            <!-- Middle Column section -->
            <!-- the form itself -->


            <?php
            if ($passed) {


//SECTION E
// define what $mysqli is made up of (taken from the fields in the html form code above)
                $mysqli = "INSERT INTO mydatabase (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, join_mailing_list)
 VALUES ('$first_name','$last_name','$phone_number','$address','$city','$state','$zip','$country','$email_address', '$comments','$join_mailing_list')";

//SECTION F
// Checking the connection and verifying the user's information/$mysqli (which was defined above) has been entered into the new record in the database, then it gives a success message
                if (mysqli_query($connect, $mysqli)) {
                    echo "New record created successfully";
                } else {
                    echo "Error: " . $mysqli . "<br>" . mysqli_error($connect);
                }


// SECTION G
// define what $email, and $email Subject is made up of for when it sends an email to me to let me know someone just posted something.
                $email = "myemail@gmail.com";
                $emailSubject = "Inquiry from the Contact Page";
// define what the headers of the email should say
                $headers = "From:$email_address\r\n";
                $headers = "Content-type: text/html\r\n";
// define what the body of the email should say and look like
                $body = <<<EOD
<br><hr><br>
First Name : $first_name <br>
Last Name : $last_name <br>
Phone Number : $phone_number <br>
Address : $address <br>
City : $city <br>
State : $state <br>
Zip : $zip <br>
Country : $country <br>
Email Address : $email_address <br>
Comments : $comments <br>
Subscribe to Mailing List : $join_mailing_list <br>
EOD;

                // SECTION H
                // define the action to take upon success of a new record having been created - i.e. mail it with the email info, email subject, body, and headers from the above definitions, to my email address
                $success = mail($email, $emailSubject, $body, $headers);
                // define the action to take upon success of a new record having been created - i.e. mail it with the email info, email subject, body, and headers from the above definitions, to the user's email address
                $usersuccess = mail($email_address, $emailSubject, $body, $headers);

                // SECTION I
                // on this same user's page on his computer in the browser window, next, print on screen a copy of what the user entered above
                echo "<h2>Thank you your message was received successfully. Here is a copy of what you sent us:</h2>";
                echo $first_name;
                echo "<br>";
                echo $last_name;
                echo "<br>";
                echo $phone_number;
                echo "<br>";
                echo $address;
                echo "<br>";
                echo $city;
                echo "<br>";
                echo $state;
                echo "<br>";
                echo $zip;
                echo "<br>";
                echo $country;
                echo "<br>";
                echo $email_address;
                echo "<br>";
                echo $comments;
                echo "<br>";
                echo $join_mailing_list;

                // SECTION J
                // when everything has been done, close the database connection
                // NOTE I am getting a red error note on the Dreamweaver software panel saying there is something wrong with this being here.. why?!?!?!?!
                $first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $join_mailing_list = "";
            }
        }
        ?>
        <p><span class="error">* required field.</span></p>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="application/x-www-form-urlencoded">
            First Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="First Name" name="first_name" value="<?php echo $first_name; ?>"><span class="error">* <?php echo $first_nameErr; ?></span><br>

            Last Name:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Last Name" required name="last_name" value="<?php echo $last_name; ?>"><span class="error">* <?php echo $last_nameErr; ?></span><br>

            Phone:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Phone" required name="phone_number" size="50" value="<?php echo $phone_number; ?>"><span class="error">* <?php echo $phone_numberErr; ?></span><br>

            Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Address" required name="address" value="<?php echo $address; ?>"><span class="error">* <?php echo $addressErr; ?></span><br>

            City:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="City" required name="city" value="<?php echo $city; ?>"><span class="error">* <?php echo $cityErr; ?></span><br>

            State:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="State" required name="state" value="<?php echo $state; ?>"><span class="error">* <?php echo $stateErr; ?></span><br>

            Zip Code:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Zip" required name="zip" value="<?php echo $zip; ?>"><span class="error">* <?php echo $zipErr; ?></span><br>

            Country:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Country" required name="country" value="<?php echo $country; ?>"><span class="error">* <?php echo $countryErr; ?></span><br>

            Email Address:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Email Address" required name="email_address" value="<?php echo $email_address; ?>"><span class="error">* <?php echo $email_addressErr; ?></span><br>

            Comment:<input class="w3-input w3-padding-16 w3-border" type="text" placeholder="Message" required name="comments" size="75" value="<?php echo $comments; ?>"><span class="error">* <?php echo $commentsErr; ?></span><br>

            Would you like to be on our mailing list?
            <input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list == "yes") echo "checked"; ?> value="yes"><label>Yes</label>
            <input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list == "no") echo "checked"; ?> value="no"><label>No</label><span class="error">* <?php echo $join_mailing_listErr; ?></span>
            <br>
            <input class="w3-input w3-padding-16 w3-border" type="datetime-local" required name="date" value="2017-11-16T20:00">
            <!--- PLACEHOLDER Not allowed on this type ABOVE-->
            <br>
            <button class="w3-button w3-black"><input type="submit" name="submit" value="Submit"></button>
        </form>

        <?php
        mysqli_close($connect);
        ?>
    </body>
</html>

I moved form to bottom, it woks from my testing. you'll have decide if you want to hide or show on successful database table insertion and email sent

Share this post


Link to post
Share on other sites

Thank you! I understand.  That Heredoc issue made a world of difference! wow!

Thanks to you both for the support on this.  I believe I owe you a coffee :) 

Thanks also for the info on the $passed. I like that piece of code in there :), and the debugging thing.

I am guessing I can comment the debugger out now?

 

I hope you both have an awesome day!

 

Peace

B

Share this post


Link to post
Share on other sites

Oh... hang on a second...

The radio button thing isn't working right. ugh!

 

I just noticed that if I click yes or no both times it just enters a zero in teh mysqli database field.  ideas? as to why this is happening?  THanks

Barb

Share this post


Link to post
Share on other sites

It would only add a '0' if the field was of a similar type to integer, as it requires a numerical value not text 'yes' or 'no'.

Share this post


Link to post
Share on other sites

Hmmm.. I think I wasn't clear in my statement before...

It doesn't matter which radio button is chosen, the database field for both yes and no are entered with zeros, both, each time.  so I have no way to differentiate if it's a yes or no that the user has entered.

 

 

 

Edited by emcy

Share this post


Link to post
Share on other sites

Hmmm, I think I wasn't clear in my statement before....

if you click either one and it outputs 'yes' or 'no', and it shows up as 'yes' or 'no' by

echo $join_mailing_list;

This has already passed the database insertion, so it can't be '0' when it is processed, it only maybe reverts to '0' if it does not receive the value, and by default is to show '0' because of table field name mistake, OR the field is not a 'char' or 'varchar' type that accepts text values,

join_mailing_list	char(4)

but a number type

join_mailing_list	int(4)

which only accepts numbers, which will return '0' instead of these text values 'yes' or 'no'.

you could change values of inputs to value 0 for 'no', 1 for 'yes'

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Yes! Thank you! That's it!  I had it set to Tiny Int (1). I am still new to mysqli and have studied the w3schools pages but still have more to learn . Thank you!!!!! Where can I send you a coffee?

Peace

B

Share this post


Link to post
Share on other sites

oh I have one more issue... When the user enters the phone number it shows up wrong in the database... i.e. when a dash is entered between the numbers 224-578-9562, then only the 224 shows up in teh database.  when I leave the dashes out, it shows up as a completely different number 2147483671 . (there are enough digits in this but it's not the number the user entered...  I am guessing this is because I have no error testing set up on the phone number in the php form?  I also have the function to strip out hash marks and other characters I don't want. .so how do I specify that for phone numbers it's ok to accept dashes, and how do I make it also give the correct number if the user chooses not to enter dashes?

Thanks

Share this post


Link to post
Share on other sites

Oh one more question......

I am also building a mailinglist page in php.

On the contact page as the last choice for the user, I give them the option to elect yes or no if they want to sign up for the mailing list.

I would like to make it so that:

1) if the user selects yes, that it will present a drop down menu to select which list they want to sign up on (I have about 8) - Because I am working with php and html, I am a bit confused... can I simply create a drop down menu in html, and then in php add an" if/else" statement so that the menu is hidden until they select yes? I feel like I'm missing a piece here... how to hide the menu until "yes" is selected? Or do I need to use Php object rules?, or.....I found a new one on the w3 schools forum about...PHP resource!  That sounds like it might be able to put the info into the other database.. ugh!  So many options! LOL!

2) How do I send all  the information from the contact page to the mailing list database (which is a separate database from teh contactpage database) only when the user selects a mailing list?

3) How to make it "echo" back on the page which mailing lists they signed up for ( more than one hopefully)

 

Thanks

Edited by emcy

Share this post


Link to post
Share on other sites

Ok.. am just processing thoughts here in english to get to the php codeing of it.....Am open to your suggestions please 

1. Php If/else statement:     If the user clicks on the "yes" radio button, then a hoverable-drop-down-menu opens up with a selection of checkboxes for which list to sign up for 

My attempt at html multi-selection-hoverable-drop-down-button: 

.....<input class="w3-radio" type="radio" name="join_mailing_list" <?php if (isset($join_mailing_list) && $join_mailing_list == "yes") echo "checked"; ?> value="yes">
  <div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Yes</button>
    <div class="dropdown-content">

<label class="container">All Lists
  <input type="checkbox" checked="checked" value'"All LIsts">
  <span class="checkmark"></span>
</label>

<label class="container">one
  <input type="checkbox" value="one">
  <span class="checkmark"></span>
</label>

<label class="container">two
  <input type="checkbox" value="two">
  <span class="checkmark"></span>
</label>

<label class="container">three
  <input type="checkbox" value="three">
  <span class="checkmark"></span>
</label>

<label class="container">four
  <input type="checkbox" value="four">
  <span class="checkmark"></span>
</label>
  </div>
</div>

My Attempt at the Php code for it:

// This would be added to Section C from my previous messages above
// this is already in the page
// if (empty($_POST["join_mailing_list"])) {
//    $join_mailing_listErr = "requires a yes or no";
//      $passed = false;
//  } else {
//    $join_mailing_list = test_input($_POST["join_mailing_list"]);
// . }"

// Here I am adding the extra new code.. some of which seems like the same thing put in here but twice (since I copied it from the php code above to re-enter it for to send the same info to the second database)....
// I want to tell it....if value="yes", then "on submit", send the page information to two databases. 
// The first database ($database) gets all the information and "joining_the_mailinglist" is shown as yes or no ... this already is set up and works.  This is done via my "Configfile.php" which is set up as an "include" in the contact form at the top.
// The second database ($mailinglistdatabase?) gets all the information plus the checked boxes of mailing lists selected. 
// so....can I be connected to two databases at one time to do this? or how do I work this the best? Do I close the first one first, then re-send the info to the second database using similar php coding to include all the info from the form plus more?

include('dbConfigmailinglist#2signininfo.php');
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (!$connect) {
    die("Connection failed: " . mysqli_connect_error());
 }

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
 }    

$first_nameErr = $last_nameErr = $phone_numberErr = $addressErr = $cityErr = $stateErr = $zipErr = $countryErr = $email_addressErr = $commentsErr = $join_mailing_listErr = "";
$first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $one = $two = $three = $four = "";

 if ($_SERVER["REQUEST_METHOD"] == "POST") { // if connected to the server successfully and it requests info or is ready to receive the info....
$passed = true;

if (empty$_POST["join_mailing_list"])) {
    $join_mailing_listErr = "requires a yes or no";
      $passed = false;
  } else {
    $join_mailing_list = test_input($_POST["join_mailing_list"]);
 }

if ($passed) {

$mysqli = "INSERT INTO mailinglistdatabases (first_name, last_name, phone_number, address, city, state, zip, country, email_address, comments, one, two, three, four)
 VALUES ('$first_name','$last_name','$phone_number','$address','$city','$state','$zip','$country','$email_address', '$comments','$one','$two','$three','$four')";


if (mysqli_query($connect, $mysqli)) {
    echo "New record created successfully";
 } else {
    echo "Error: " . $mysqli . "<br>" . mysqli_error($connect);
 }

$email = "info@myemail.com";
 $emailSubject = "Inquiry from the contact page with Newsletter signup";

 // define what the headers of the email should say
 $headers = "From:$email_address\r\n";
 $headers = "Content-type: text/html\r\n";

 // define what the body of the email should say and look like
 $body = <<<EOD
 <br><hr><br>
 First Name : $first_name <br>
 Last Name : $last_name <br>
 Phone Number : $phone_number <br>
 Address : $address <br>
 City : $city <br>
 State : $state <br>
 Zip : $zip <br>
 Country : $country <br>
 Email Address : $email_address <br>
 Comments : $comments <br>
 You subscribed to the following Newsletters :  // how do I show which ones here?<br>

<br>
EOD;

echo "<h2>Thank you your message was received successfully. Here is a copy of what you sent us:</h2>";
 echo "First Name: " . $first_name . "<br>";
 echo "Last Name: " . $last_name . "<br>";
 echo "Phone Number: " . $phone_number . "<br>";
 echo "Address: " . $address . "<br>";
 echo "City: " . $city . "<br>";
 echo "State: " . $state . "<br>";
 echo "Zip Code: " . $zip . "<br>";
 echo "Country: " . $country . "<br>";
 echo "Email Address: " . $email_address . "<br>";
 echo "Comments: " . $comments . "<br>";
 echo "Joined The following Mailing lists: " . // $how do I show which ones here?;

$first_name = $last_name = $phone_number = $address = $city = $state = $zip = $country = $email_address = $comments = $one = $two = $three = $four = "";
            }
        }
 ?> 

$mailinglistdatabase = $first_name','$last_name','$phone_number','$address','$city','$state','$zip','$country','$email_address', '$comments','one',two','three','four'

// The form's html code would go in here?
<form></form>

 

Issues:

  • do I need to define a new $variable here for the entire page of info? to send to the "mailinglist" database? , or perhaps use another "Insert Into" statement for the second database?  Is it possible to nest "Insert Into" statements?
  • Is it possible to nest "include" statements for two databases?

Thanks

B

 

Share this post


Link to post
Share on other sites

You can't show/hide anything with PHP on its own once a page is fully loaded. PHP is sent to server, processed and returned as html and content. JavaScript works client-side through the browser as long as JavaScript is enabled on the browser. You can however access a PHP page through JavaScript using AJAX, you can then display the returned html + content produced by the PHP file and place it where you require. ALL without the reloading of the page.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×