Jump to content

PHP Assistance


spudly1987
Go to solution Solved by debascoguy,

Recommended Posts

Hey everyone I am a newbie looking for some good assistance and support. 

 

The Questions are at the bottom of the page.
 
I am using
Adobe Dreamweaver Cs6
&
Xampp
 
I have the following two codes written up  
 
HTML Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
 .labelclass{
  float: left;
  width: 150px;
 }
</style>
</head>
<body>
<img src="http://omgtechhelp.com/wp/wp-content/themes/OMGTech/images/logo7small.jpg" />
<center>
<h1><u>Notes For The Month Of August</u></h1>
</center>
<hr />
<form name="myform" action="http://localhost/site/results.php" target="_blank" method="post">
<span class="labelclass" style="font-family:'Comic Sans MS', cursive">Customer Name: </span><input type="text" name="name" /><br />
<span class="labelclass" style="font-family:'Comic Sans MS', cursive">Phone Number: </span><input type="tel" name="pnumber" /><br />
<span class="labelclass" style="font-family:'Comic Sans MS', cursive">E-Mail Address: </span><input type="text" name="eaddy" /><br />
<span class="labelclass" style="font-family:'Comic Sans MS', cursive">Issue: </span><input type="text" name="issue" /><br />
<span class="labelclass" style="font-family:'Comic Sans MS', cursive">Results: </span><input type="text" name="results" /><br />
<span class="labelclass" style="font-family:'Comic Sans MS', cursive">Date: </span><input type="date" name="Date" /><br />
<input type="submit" name="submit" value="submit" style="background-color:#F60" />
</body>
</html>

PHP Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

</head>

<body>
<?php
if($_POST["submit"])
{
    $customername = $_POST["name"];
    $phonenumber = $_POST["pnumber"];
    $email = $_POST["eaddy"];
    $issue = $_POST["issue"];
    $result = $_POST["results"];
    $date = $_POST["Date"];
    
    
    echo "Customer Name: $customername<br />
    Phone Number: $phonenumber<br />
    E-Mail Address: $email<br />
    Issue: $issue<br />
    Results: $result<br />
    Date: $date<br />"
;}
?>
</body>
</html>

There are Three things I would like to accomplish 

 

1. when I click on the submit button i would like the data to be put into table format as such, which also means i need to know how to have the php saved every time so that the information doesnt get over written 

 

Customer 1       Customer 2       Customer 3    Customer 4

 

Customer 5       Customer 6      Customer 7    Customer 8

 

etc 

 

2. I would like to be able to make the phone number field automatically convert in to (111) 111-1111 Format 

3. for the result input field I want to know how to make it when i submit i can have it change to green for completed, or blue for shiftchange, or etc. 

 

Any assistance would be greatly appreciated, i am a quick learner but i do like to ask a lot of questions, if it is easier for you just to do the code that is okay for me, but if you do i would like for you to explain what you did and why so i can also learn

 

Thank you in advanced. 

Link to comment
Share on other sites

1. when I click on the submit button i would like the data to be put into table format as such, which also means i need to know how to have the php saved every time so that the information doesnt get over written.

 

OK, I think what you are saying is that you want the user to keep submitting records and have the newly created record and the previously submitted records display. Well, that would assume that you need to SAVE the data. This is typically done with a database. Trying to teach you how to set up a database and do all the CRUD operations (create, retrieve, update, & delete) cannot be done efficiently within a forum post. There are plenty of tutorials out there to get you started. But, as a general explanation, you would take the newly submitted record, run it through whatever validation checks you need to do, then add the necessary record(s) to the DB using an INSERT query. Then you would run a SELECT query to get all the records that have been saved and run a loop to create the output.

 

 

2. I would like to be able to make the phone number field automatically convert in to (111) 111-1111 Format 

 

There are multiple ways to achieve this. As part of the code to add records I would strip the phone input of all non-alphanumeric characters and then validate the length to whatever you deep appropriate. Some would say to strip all non-numeric characters, but I like to give the user the option to use letters. So, you would store the raw, unformatted value in the DB. Here is an example of how the code to validate the phone might look

 

$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
$phone = preg_replace("#[^a-z0-9]#i", '', $phone);
if(strlen($phone) < 10)
{
    //Error must enter a 10 character phone number
}
else
{
    //Phone value is OK to save
}

 

Then, when you display the phone later you can format the values however you want using string functions.

while($row = mysql_fetch_assoc($database_result))
{
    $displayPhone = '(' . substr($row['phone'], 0, 3) . ') ' . substr($row['phone'], 0, 3) . '-' . substr($row['phone'], 0, 3);
}

 

 

3. for the result input field I want to know how to make it when i submit i can have it change to green for completed, or blue for shiftchange, or etc. 
 
No idea what you are talking about. It sounded as if you want the user to re-use the form over and over to continue adding values. So, changing the color of the form elements based upon the last submission doesn't seem right if the user is going to submit another value.
Link to comment
Share on other sites

Hey, here is a way to code to submit to your database table.

  1. <?php
  2. //Protect against mysql_injection
  3.  
  4. $customername = mysql_real_escape_string(trim($_POST["name"]));
  5. $phonenumber = mysql_real_escape_string(trim($_POST["pnumber"]));
  6. $email = mysql_real_escape_string(trim($_POST["eaddy"]));
  7. $issue = mysql_real_escape_string(trim($_POST["issue"]));
  8. $result = mysql_real_escape_string(trim($_POST["results"]));
  9. $date = mysql_real_escape_string(trim($_POST["Date"]));
  10.  
  11. //Now check form input(Validating the form).
  12. $errmsg_arr = array(); //Array to store validation errors
  13. $check_Error = false; //Validation error flag
  14.  
  15. if (empty($customername)){
  16. $errmsg_arr[]= '.Please Enter Your Name';
  17.  $check_Error = true;
  18. }
  19. if (empty($phonenumber)){
  20. $errmsg_arr[]= '.Please Enter Your Phone Number';
  21.  $check_Error = true;
  22. }
  23. if (empty($email)){
  24. $errmsg_arr[]= '.Please Enter Your Email';
  25.  $check_Error = true;
  26. }
  27. if (empty($issue)){
  28. $errmsg_arr[]= '.Please what is your issue';
  29.  $check_Error = true;
  30. }
  31. if (empty($result)){
  32. $errmsg_arr[]= '.Please what is your issue';
  33.  $check_Error = true;
  34. }
  35. if (empty($date)){
  36. $errmsg_arr[]= '.Please what is your issue';
  37.  $check_Error = true;
  38. }
  39. //Printing out any error message stored in the array.
  40. if ($check_Error == true){
  41.   echo '<h1>ERROR: </h1><h3>Please check below for Error Details</h3>';
  42.  
  43.   if( isset($errmsg_arr) && is_array($errmsg_arr) && count($errmsg_arr) > 0 ) {
  44.       echo '<ul><font color="red">';
  45.       foreach($errmsg_arr as $msg) {
  46.         echo '<li><b>Error:    '.$msg.'</b></li><br />';
  47.        }
  48.          echo '</font></ul>';
  49.    }
  50.       //Please change the a href link to the name of your page.
  51.       echo "<p><a href='homepage_registration_form.html'>Go Back To Register</a></p>";
  52. }
  53. //After validating successfully
  54. else {
  55. /* Now we will write a query to insert user details into database */
  56. $host = "localhost"; // Host name...change it to your configuration information.
    $username = "root"; // Mysql username...change it to your configuration information.
    $password = ""; // Mysql password...change it to your configuration information.
    $db_name = "mydb"; // Database name...change it to your configuration information.
    // Connect to server
    mysql_connect("$host", "$username", "$password") or die('ERROR: Cannot connect' .mysql_error());
    //connect to database
    mysql_select_db("$db_name") or die ('ERROR: Cannot connect'.mysql_error());
  57.  
  58. $tbl_name = "UserNote";  //Mysql Table name...change it to your configuration information.
  59.  
  60. $sql="INSERT INTO $tbl_name (CustomerName, Phone, Email, Issue, Result, Date)
            VALUES($customername', '$phonenumber', '$Email',  '$issue', '$result', '$date')";
  61.  
  62. if ( ! mysql_query($sql) ) //notice the "!" it means if the mysql_query($sql)  cannot be executed, then die error. ELSE execute the mysql_querry($sql) to
  63.                                         //insert into table in the database.
  64. {   
  65. die('Error in Registration,: ' . mysql_error());
  66. }
  67. else
    {    //Insert User into the database.
  68. echo "Your Note was insert into the record successfully, Please below are your information entered in the database.";
  69. echo "Customer Name: $customername<br />
  70. Phone Number: $phonenumber<br />
  71. E-Mail Address: $email<br />
  72. Issue: $issue<br />
  73. Results: $result<br />
  74. Date: $date<br />"
  75. echo "......Stay cool and Goodluck.";
  76. ?>
  77. You should use css to change the field color.. check here for more tutorials on php and css.....www.w3schools.com.
Edited by debascoguy
Link to comment
Share on other sites

  • Solution

 

Hey, here is a way to code to submit to your database table.

  1. <?php
  2. //Protect against mysql_injection
  3.  
  4. $customername = mysql_real_escape_string(trim($_POST["name"]));
  5. $phonenumber = mysql_real_escape_string(trim($_POST["pnumber"]));
  6. $email = mysql_real_escape_string(trim($_POST["eaddy"]));
  7. $issue = mysql_real_escape_string(trim($_POST["issue"]));
  8. $result = mysql_real_escape_string(trim($_POST["results"]));
  9. $date = mysql_real_escape_string(trim($_POST["Date"]));
  10.  
  11. //Now check form input(Validating the form).
  12. $errmsg_arr = array(); //Array to store validation errors
  13. $check_Error = false; //Validation error flag
  14.  
  15. if (empty($customername)){
  16. $errmsg_arr[]= '.Please Enter Your Name';
  17.  $check_Error = true;
  18. }
  19. if (empty($phonenumber)){
  20. $errmsg_arr[]= '.Please Enter Your Phone Number';
  21.  $check_Error = true;
  22. }
  23. if (empty($email)){
  24. $errmsg_arr[]= '.Please Enter Your Email';
  25.  $check_Error = true;
  26. }
  27. if (empty($issue)){
  28. $errmsg_arr[]= '.Please what is your issue';
  29.  $check_Error = true;
  30. }
  31. if (empty($result)){
  32. $errmsg_arr[]= '.Please what is your issue';
  33.  $check_Error = true;
  34. }
  35. if (empty($date)){
  36. $errmsg_arr[]= '.Please what is your issue';
  37.  $check_Error = true;
  38. }
  39. //Printing out any error message stored in the array.
  40. if ($check_Error == true){
  41.   echo '<h1>ERROR: </h1><h3>Please check below for Error Details</h3>';
  42.  
  43.   if( isset($errmsg_arr) && is_array($errmsg_arr) && count($errmsg_arr) > 0 ) {
  44.       echo '<ul><font color="red">';
  45.       foreach($errmsg_arr as $msg) {
  46.         echo '<li><b>Error:    '.$msg.'</b></li><br />';
  47.        }
  48.          echo '</font></ul>';
  49.    }
  50.       //Please change the a href link to the name of your page.
  51.       echo "<p><a href='homepage_registration_form.html'>Go Back To Register</a></p>";
  52. }
  53. //After validating successfully
  54. else {
  55. /* Now we will write a query to insert user details into database */
  56. $host = "localhost"; // Host name...change it to your configuration information.

    $username = "root"; // Mysql username...change it to your configuration information.

    $password = ""; // Mysql password...change it to your configuration information.

    $db_name = "mydb"; // Database name...change it to your configuration information.

    // Connect to server

    mysql_connect("$host", "$username", "$password") or die('ERROR: Cannot connect' .mysql_error());

    //connect to database

    mysql_select_db("$db_name") or die ('ERROR: Cannot connect'.mysql_error());

  57.  
  58. $tbl_name = "UserNote";  //Mysql Table name...change it to your configuration information.
  59.  
  60. $sql="INSERT INTO $tbl_name (CustomerName, Phone, Email, Issue, Result, Date)

            VALUES($customername', '$phonenumber', '$Email',  '$issue', '$result', '$date')";

  61.  
  62. if ( ! mysql_query($sql) ) //notice the "!" it means if the mysql_query($sql)  cannot be executed, then die error. ELSE execute the mysql_querry($sql) to
  63.                                         //insert into table in the database.
  64. {   
  65. die('Error in Registration,: ' . mysql_error());
  66. }
  67. else

    {    //Insert User into the database.

  68. echo "Your Note was insert into the record successfully, Please below are your information entered in the database.";
  69. echo 'Customer Name: $customername' . '<br />' . 'Phone Number: $phonenumber' . '<br />' . 'E-Mail Address: $email' . '<br />';
  70. echo  'Issue: $issue' . '<br />' . 'Results: $result' . '<br />' . 'Date: $date' . '<br />' ;
  71. echo "......Stay cool and Goodluck.";
  72. ?>
  73. You should use css to change the field color.. check here for more tutorials on php and css.....www.w3schools.com.

 

Edited by debascoguy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.