Link to home
Start Free TrialLog in
Avatar of Redscrapbook
RedscrapbookFlag for Ireland

asked on

PHP connection with 'localhost' for ajax form validation.

I've this php script as below.

First question: I'm using MAMP as 'localhost' 'root' and how do I connect or add 'localhost' 'root' in php as below? In order for form validation to work? I'm using Dreamweaver CC5.5.

Second question: I'll use collegeData.sql as well. Do I need collegeData.sql? The Username is studentID and that will end up in collegeData.sql

My database information is:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'collegeData.sql';



My problem php is:
<?php

if (@$_REQUEST['action'] == 'check_username' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
    echo json_encode(check_username($_REQUEST['username']));
    exit; // only print out the json version of the response
}
?>

The HTML code is
<!DOCTYPE HTML>
<html lang="en">
      <head>
      <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
      <title>jQuery for Designers - Ajax Form Validation Example</title>
      <style type="text/css" media="screen">
<!--
BODY {
&#9;margin: 10px;
&#9;padding: 0;
&#9;font: 1em "Trebuchet MS", verdana, arial, sans-serif;
&#9;font-size: 100%;
}
H1 {
&#9;margin-bottom: 2px;
&#9;font-family: Garamond, "Times New Roman", Times, Serif;
}
TEXTAREA {
&#9;width: 80%;
}
INPUT, TEXTAREA {
&#9;font-family: Arial, verdana;
&#9;font-size: 125%;
&#9;padding: 7px;
}
LABEL {
&#9;display: block;
&#9;margin-top: 10px;
}
IMG {
&#9;margin: 5px;
}
#message {
&#9;border: 1px solid #ccc;
&#9;background-color: #ffa;
&#9;padding: 5px;
}
DIV.submit {
&#9;background: #eee;
&#9;border: 1px solid #ccc;
&#9;border-top: 0;
&#9;padding: 1em;
&#9;text-align: right;
&#9;margin-bottom: 20px;
}
-->
</style>
      <script src="jquery.js" type="text/javascript"></script>
      <script type="text/javascript">
      <!--
      $(document).ready(function () {
          // Username validation logic
          var validateUsername = $('#validateUsername');
          $('#username').keyup(function () {
              // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
              var t = this; 
              
              // only run the check if the username has actually changed - also means we skip meta keys
              if (this.value != this.lastValue) {
                  
                  // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                  // a particular key, it will only fire when the release the key.
                                  
                  if (this.timer) clearTimeout(this.timer);
                  
                  // show our holding text in the validation message space
                  validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
                  
                  // fire an ajax request in 1/5 of a second
                  this.timer = setTimeout(function () {
                      $.ajax({
                          url: 'ajax-validation.php',
                          data: 'action=check_username&username=' + t.value,
                          dataType: 'json',
                          type: 'post',
                          success: function (j) {
                              // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                              validateUsername.html(j.msg);
                          }
                      });
                  }, 200); // this 200? It can be removed.
                  
                  // copy the latest value to avoid sending requests when we don't need to
                  this.lastValue = this.value;
              }
          });
      });
      //-->
      </script>
      </head>
      <body>
      <div>

&#9;&#9;<form action="" method="post">
            <fieldset>
        <label for="username">Username, valid: a-z.-_</label>
        <input type="text" name="username" value="<?=@$_REQUEST['username']?>" id="username" />
        <span id="validateUsername">
  &#9;&#9;<?php if ($error) { echo $error['msg']; } ?>
  &#9;&#9;</span>
&#9;&#9;</fieldset>
 &#9;&#9;<input type="hidden" name="action" value="register" />
&#9;&#9;<input type="submit" name="register" value="Register" id="register" />
&#9;&#9;<!-- End form-->
&#9;&#9;</form>
        </div>

&#9;</body>
&#9;</html>

Open in new window

Avatar of chrisroch
chrisroch

First Question

I am not 100% clear as to what you are asking here. Are you trying to find out how to connect to a database? If so, I would advice you to check out tutorials on the web that can give you details for connecting to a database from PHP,  for e.g. http://www.w3schools.com/php/php_mysql_intro.asp provides a decent tutorial for using php alongside mysql.


Second Question

Based on your question I will assume you are carrying out a username check against a database. However, it seems as if you have not created a database as yet, since you have set the database variable to the name of your SQL script file "collegeData.sql".  Ensure that you have created a database on your server that will be used by your application, and set the database variable in your script with the name of said database. You can refer to the link I provided above to guide you in this process.
Avatar of Redscrapbook

ASKER

Second Question

The data is already in MySQL and it is stored in database objects in three moduleTable, lecturerTable and studentsTable from collegeData.mysql. I would appreciate if you can point out one example for moduleTable in a php script so I can fully be clear.

------------------------------------------------------
|  studentID  | firstName| lastName  |  
------------------------------------------------------
| 123             | Kermit      | Frog           |
| 124             | Gonzo      |Great           |
| 125             | Cookie     | monster      |
------------------------------------------------------
Avatar of Dave Baldwin
You have many thing wrong there.  First, do you get the default web page when you go to "http://locahost/" ?

Second, 'collegeData.sql' is not the Name of the database though 'collegeData' might be.  'collegeData.sql' should be the file that you imported into MySQL to create the database.

Third, $_SERVER['HTTP_X_REQUESTED_WITH'] is an unreliable variable.  It isn't necessarily set properly or preserved properly.

Fourth, there is no database access in the 'php page' you posted.  All you are doing is echoing something you're sending to the page.

If you need it, here http://us2.php.net/manual/en/book.mysql.php is the PHP page on using 'mysql'.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@routinet: Regarding the use of the root account, this kind of set up is common in classroom settings where each student has a Mac and an individual MAMP set up on his computer.  It would be a security exposure in the "real world" but in the classroom, the entire build for applications like these consists of copying the script into htdocs/ and clicking on the name.  Probably nobody else uses the computer, and probably the data model gets wiped clean every night.

:-)

~Ray
@Ray: very true, but still good advice.

I was also going to point out that one of the things wrong here was the use of Dreamweaver, but that could just be me. ;)
@routinet:  A few histrionics here, but also a few good points.
http://w3fools.com/ 

They have a comment on Dreamweaver.  I agree about the use of Dreamweaver.  It's led too many would-be programmers into a morass of horribly written PHP code, with the result that they render themselves unemployable at any place that does not use Dreamweaver (ie: they are permanently excluded from all the high-value jobs).
Thank you to Ray and Routinet for the input. Routinet for test the connectivity and and it works, I was able to see instinct results.