Results 1 to 2 of 2

Thread: seperating client side from sever side

  1. #1
    Join Date
    Aug 2013
    Posts
    86
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default seperating client side from sever side

    Am testing login via json php using mysql deprecated.
    This code works excellent when all is runing on the sever side.

    now i have seperated the client side from the server side.
    The server side which is login.php is hosted on my web hosting account
    login.config
    Code:
    <?php
     class db{
     function connect(){
     mysql_connect("localhost","root","");
                            mysql_select_db("log_db");
     }
     }
    ?>
    Code:
    <?php
     session_start();
     require 'mdb.php'; // link to your database class
     $db = new db;
     $json = array();
     if(isset($_REQUEST['email']) && isset($_REQUEST['password'])){
     $email = $_REQUEST['email'];
     $password = $_REQUEST['password'];
     $db->connect();
     $sql = sprintf("SELECT id FROM members WHERE email='%s' AND password='%s'",mysql_real_escape_string($email),$password);
     $sql = mysql_query($sql) or die(mysql_error()); 
     if(mysql_num_rows($sql)){
     $uid = mysql_result($sql,0);
     $_SESSION['id'] = $uid;
     $json['status'] = true;
     $json['message']= "Logging in...";
     }else{
     $json['status'] = false;
     $json['message']= "Email and password did not match.";
     }
     }
     echo json_encode($json);
    ?>

    while my client side is stiil on my local system.
    json.js
    Code:
    $(function(){
     $('#login').submit(function(){
     $.ajax({
     url: 'http://example.com/login.php',
     data: $(this).serialize(),
     dataType: 'json',
     beforeSend: function(){$('button').after('<img src="throbber.gif" id="throbber"/>');},
     success: function(response){
     $('#throbber').fadeOut();
     if(response.status){ 
     $.when(function(){
     $('#responseText').css({'border':'1px solid #A2D246','background':'#EBF8A4'}).html(response.message).fadeIn('fast');
     }).then(function(){
     window.location.href='myaccount.php';
     });
     }else{
     $('#responseText').css({'border':'1px solid #F69','background':'#FCC'}).text(response.message).fadeIn('fast');
     }
     }
     });
     return false;
     });
    });
    Code:
    <html><head>
       
    
       <script src="json.js">
            </script>
    </head><body>
    
    
    <div id="responseText" style="padding:5px;display:none;word-wrap:break-word;margin:0 0 5px 0;"></div>
    
            <form action="" method="post" id="login">
            <table>
                <thead>
                    <tr><th colspan="2">Customer Login</th></tr>
                </thead>
                <tbody>
                    <tr><td align="right">Email:</td><td><input type="text" name="email" /></td></tr>
                    <tr><td align="right">Password:</td><td><input type="password" name="password" /></td></tr>
                    <tr><td> </td><td><button type="submit">Login</button></td></tr>
                </tbody>
            </table>
            </form>
    </body></html>
    Am trying to use the client side which is on the local system with me to connect to login.php which on the internet on my hosting account but its not working. it seems i cannot connect to login.php

    what is wrong. Can't i use json this way

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Same origin policy. You cannot use AJAX this way:

    Code:
    $(function(){
     $('#login').submit(function(){
     $.ajax({
     url: 'http://example.com/login.php',
    unless that (above) code is on http://example.com

    But you seem to indicate that it's on the localhost. If so, it will be access denied for an AJAX call to http://example.com.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Breaking Client side validations
    By letom in forum JavaScript
    Replies: 2
    Last Post: 03-13-2013, 07:47 PM
  2. how api works client side
    By morplug in forum JavaScript
    Replies: 3
    Last Post: 10-03-2011, 02:39 AM
  3. Need help playing sound on the client side
    By tvin_98 in forum Looking for such a script or service
    Replies: 1
    Last Post: 05-06-2010, 12:49 AM
  4. can this be done client side
    By Steve Sea in forum Looking for such a script or service
    Replies: 5
    Last Post: 12-03-2007, 10:35 PM
  5. downloading to client side
    By aniltc in forum PHP
    Replies: 5
    Last Post: 12-01-2006, 08:31 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •