You are viewing our Forum Archives. To view or take place in current topics click here.
PHP won't output to screen
Posted:

PHP won't output to screenPosted:

ApexGenesisUK
  • TTG Senior
Status: Offline
Joined: Aug 25, 201013Year Member
Posts: 1,059
Reputation Power: 44
Status: Offline
Joined: Aug 25, 201013Year Member
Posts: 1,059
Reputation Power: 44
When i click submit on the form it doesn't output any information from the php and upload to the database.

Link: [ Register or Signin to view external links. ]

HTML:
<fieldset style="width:30%"><legend>Booking-Form</legend>
<table border="0">
<tr>
<form method="POST" action="connectivity-sign-up.php">
<td>Full Name</td><td> <input type="text" name="name"></td>
</tr>
<tr>
<td>Email</td><td> <input type="text" name="email"></td>
</tr>
<tr>
<td>Date</td><td> <input type="date" name="date"></td>
</tr>
<tr>
<td>Time</td><td> <input type="time" name="time"></td>
</tr>
<tr>
<td>Activity</td><td><input name="message" type="text" value=""></td>
</tr>
<tr>
<td><input id="button" type="submit" name="submit" value="Book-Now!"></td>
</tr>
</form>
</table>
</fieldset>
</div>
</body>
<footer>
<address>&copy; Copyright 2015 All Rights Reserved</address>
</footer>
</html>

PHP:

<?php


define('DB_HOST', 'localhost');
define('DB_NAME', 'my_webberbridge');
define('DB_USER','webberbridge');
define('DB_PASSWORD','');

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());


function Book()
{
$FullName = $_POST['name'];
$Date = $_POST['date'];
$Email = $_POST['email'];
$Time = $_POST['time'];
$query = "INSERT INTO bookform (Fullname,date,email,time) VALUES ('$fullname','$Date','$Email','$Time')";
$data = mysql_query ($query)or die(mysql_error());
if($data)
{
echo "Successfully Submitted To Database, Thankyou For Your Time";
}
}



?>
#2. Posted:
iyop45
  • Prospect
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
Status: Offline
Joined: Apr 15, 201113Year Member
Posts: 614
Reputation Power: 83
You've defined a function but not called it.

Put Book(); at the end of your script.
#3. Posted:
Cyimking
  • V5 Launch
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
Status: Offline
Joined: May 02, 201212Year Member
Posts: 1,129
Reputation Power: 34
My suggestion because calling the function book() will give you an error..

This is the db_includes.php file

<?php
   define('DB_HOST', 'localhost');
    define('DB_NAME', 'my_webberbridge');
    define('DB_USER','webberbridge');
    define('DB_PASSWORD','');
     
    $con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_PASSWORD);
   
      // Check for errors when connecting to the DB
      if($con->connect_error)
      {
       die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);
   }
   
   /* For PHP 5.2.9 and below use this:
   if (mysqli_connect_error())
   {
       die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
   }
   */

/* Good Practice = Never end a php file with the closing tag to prevent unwanted space
* See link - http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag
*
* Obviously if you have to add in JS or HTML then you would have to use the tag...
* For example:

    // This is a simple form that's allow the user to edit it's username and email.
   // As you notice, i added a simply define function for the submit button...
   <?php
      define("SUBMIT", "Edit Account");
   ?>
   
   <!-- We are no longer in PHP so we have to use HTML5 comments -->
   <!-- The value is the current user's username / email. This is useful because it shows the user their current info -->
   <form action="#" method="post>
      <input type="text" name="edit_username" value="<?php echo $_SESSION['user']['username']; ?>" />
      <input type="email" name="edit_email" value="<?php echo $_SESSION['user']['email']; ?>" />
      <input type="submit" name="submit" value="<?php echo SUBMIT; ?>" />
   </form>

*/



This is the connectivity-sign-up.php file

// Class the handles logging in, logging out, etc... functions
<?php
include("db_includes.php");

class foo {
   
   /**********************************************
   * Purpose - Insert new book
   *
   * @param $_POST = variables from the form
   * @return query message
   ************************************************/
   public function book($_POST)
   {
      // Get connection from the DB Includes. This connection is in OOP form. 
      global $con;
      
      // Strip the inputs. However you will need to validate the date
       $fullName = mysqli_real_escape_string($con,$_POST['name']);
          $date =  mysqli_real_escape_string($con,$_POST['date']);
          $email =  mysqli_real_escape_string($con,$_POST['email']);
          $time =  mysqli_real_escape_string($con,$_POST['time']);
          $query = "INSERT INTO bookform (Fullname,date,email,time) VALUES ('$fullName','$date','$email','$time')";
          $data = $con->query($query);
       
          if($data)
      {
             return "Successfully Submitted To Database, Thank you For Your Time";
          }
      
      else
      {
         return "Sorry, we was unable to submit this to the database. Please contact an administrator or try again later";
      }
    }
}

// The actual part now! In some part, all classes and functions will be in another file.
// So at some point the class foo will be a folder called classes or some sort and you will just require_once it.
// But i'm keeping this thing simple :-)

// Check if the user's is in this page due to an HTTP POST request.
// Yes you can check if submit button is set "isset(_POST['submit'])" but this is the proper way of checking.
// 3 equal signs are used for better checking. Check PHP manual for better details.
// Note - Never check if the POST is empty due to checkboxes of forms can be nulled / empty.
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
     $foo = new $foo();                  // Initialize the foo() class and set the object to $foo
     $message = $foo->book($_POST);       // Call the book class. This will return a message
   
    //Extra stuff you can just comment out
    header("refresh: 3 ; url= " . $url);   // Redirect to some url you must set. Delay the redirect by 3 secs.
    die($message);                     // Kill the script and output the message!
}


Explanation of the code...
1) Instead of having some random function, I used a class called foo because at some point you will have multiple functions related to the login function plus why not use classes?

2) I simply copied the code from your function HOWEVER, i added a parameter that's an array of the POST variables to be on the safe side of things. You do not want any side effects when you call the function.

3. You MUST check if the user went to the page due to a POST HTTP request. Else your code will run and you will receive an error because the post array is empty BUT you are using the post variable as if it isn't empty.

4. After the check, you initialize the class then call the function with the POST array from the form as the parameter for the function book.

Notes
1. Don't use mysql_* use either PDO or mysqli*. PDO is more global but it might lack some mysqli_* functions that you MIGHT need.

2. Sanitize and strip those POST variables for security. At some point, use prepared statements.

3. Keep everything in OOP. If you are using classes then the mysqli connection should be in OOP form... so


$connection = new mysqli(SERVER, USER, PASS, DB);
$connection->query($query);


and so on.. you get the point.

4. How are you handling the form? Never rely on just JS or html5 for security. Make sure you are doing the following:

- Checking if all inputs are empty. Even if you are using html5 required... make sure you do a double check to prevent hacking!

- Clean strings / emails. You can use a built in function called sanitize to check if the email is indeed an email. You can also use the email type for the input to tell the user that he or she must enter an email for example.

- Use html5 to help on the front end. That article is super old and you are already learning bad habits!

- Add in another field to repeat passwords so the user can double check.

- I know this is a simple tutorial but you need to have good practices or you will screw something up when you actually try to build a program.

5. Have fun learning PHP!

6. I didn't run this script. So if there are some errors, let me know and i'll fix!
#4. Posted:
minecrafto
  • Powerhouse
Status: Offline
Joined: Nov 01, 201211Year Member
Posts: 484
Reputation Power: 18
Status: Offline
Joined: Nov 01, 201211Year Member
Posts: 484
Reputation Power: 18
Btw you may want to encrypt the php code after finishing.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.