skip to Main Content

I’m trying to post the data from the HTML file to localhost using xampp and it is not posting in the db. It shows that successfully added but not showing. is my server name and the URL correct or what am I doing wrong?
And how can we open it on our mobile using xampp?

HTML file comment.html

<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <title>Loading data into a Cordova app</title>
</head>
<body>

  <div data-role="page">
  <div data-role="header">
    <h1>Welcome To My Form Submit Example</h1>
  </div>
  <div data-role="main" class="ui-content">
    <form>
      <div class="ui-field-contain">
        <label for="email">
            <b>Email</b></label>
            <input type="email" id="email" name="email">
        <label for="comment">
            <b>Comment</b></label>
            <textarea id="comment" name="comment" cols="30" rows="10"></textarea>
         </div>
         <input type="submit" data-inline="true" value="Submit">
    </form> 
  </div>
  <div data-role="footer">
    <h1>Developed by Tareq Assaf</h1>
  </div>
</div>

  <script type="text/javascript" src="cordova.js"></script>
  <script type="text/javascript" src="index.js"></script>
  <script type="text/javascript">

$('form').submit(function(){
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData,
        url: 'http://ccr/comment.php',
        success: function(data){
            console.log(data);
            alert('Your comment was successfully added');
        },
        error: function(){
            console.log(data);
            alert('There was an error adding your comment');
        }
    });

    return false;
});



        </script>
</body>
</html>

index.js

$('form').submit(function(){
    var postData = $(this).serialize();

    $.ajax({
        type: 'POST',
        data: postData,
        url: 'http://ccr/comment.php',
        success: function(data){
            console.log(data);
            alert('Your comment was successfully added');
        },
        error: function(){
            console.log(data);
            alert('There was an error adding your comment');
        }
    });

    return false;
});

comment.php

$server = "localhost";
$username = "root";
$password = "";
$database = "ccr";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);


$email = mysql_real_escape_string($_POST["email"]);
$comment = mysql_real_escape_string($_POST["comment"]);

$sql = "INSERT INTO comment (email, comment) ";
$sql .= "VALUES ('$email', '$comment')";

if (!mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
} else {
    echo "Comment added";
}

mysql_close($con);

echo('Added Successfully');

phpmyadmin data photo1

2

Answers


  1. The url of the ajax seems incorrect, url: 'http://ccr/comment.php',

    It’s not a valid domain, let say if it on the local, it should be either localhost or somedomain.test some kind like that,

    Please take a note, if you point the ajax from mobile to your localhost PC, you should point it via IP Address, both must be on the same network.

    Let say your PC’s IP Address is 192.168.1.21 so your ajax must point to that 192.168.1.21/comment.php

    Login or Signup to reply.
  2. make sure you are pointing to the correct url

    url: 'http://ccr/comment.php',
    

    check whether it is

    url: 'http://localhost/ccr/comment.php',
    

    and also you cant access your localhost from other devices like your mobile etc. if thats the case you should use 127.0.0.1 or try your ip address like http://198.41.2.115/ccr/comments.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search