0

I have made an application where the user inputs their name and lastname which is then saved to the database 'register' in the 'user' table once they click on 'include'. I am completely stuck on this and can't seem to get the data saved to the database. I have added my HTML, JS and PHP code below. Can anyone help me solve what's wrong?

function.js:

$( document ).ready(function() {
    var $server;
    $server = 'http://localhost:81/xampp/';

    $('#include').on('click', function() {
        $name = $('#$name').val();
        $lastname = $('#lastname').val();

        $.ajax ({
            type: "get",
            url: $server-"/connectdb.php",
            data: "name="+$name+"&lastname="+$lastname+"&action=include",
            success: function(data) {
                intel.xdk.notification.alert('user recorded. ok!', 'Message', 'OK');

            }
        });
    });  

});

connectdb.php:

<?php
if(isset($_GET["include"])){
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "register";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO user (user_name, user_lastname)
VALUES ('".$_GET['name']."','".$_GET['lastname']."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
?>

index.html:

       <link rel ="stylesheet" href="jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="jquery.mobile-1.4.5/jquery-2.1.1.min.js"></script>
    <script src="jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <script src="js/function.js"></script>
</head>

<body>

    <!-- page -->
    <div data-role="page" id="page1">

    <!-- header -->
    <div data-role="header">
    <h1>Records</h1>

    <!-- film header -->                        
    </div>

    <!-- main -->
        <div data-role="main" class="ui-content">

            <label>Name</label>
            <input id="name" type="text" name="u_name" /> <br>

            <label>Lastname</label>
            <input id="lastname" type="text" name="u_lastname" /> <br>

            <button id="include">Include</button>

            <HR>
                <a href="#page2" class="ui-btn ui-btn-corner-all"> View Entries</a>
        </div>
            <!-- film main -->    
    </div>

    <!-- film page -->
    <!-- page 2 -->
    <div data-role="page" id="page2">

             <!-- header 2 -->
             <div data-role="header">

                              <a href="#page1" data-rel="back" class="ui-btn-left ui-btn ui-btn-icon-notext ui-corner-all ui-icon-back">Back</a>


            <h1>User List</h1>
   </div>

   <!-- film header 2 -->
   <!-- main 2 -->
             <div data-role="main" class="ui-content">
                 <p id="userlist"></p>
             </div>
        </div>
        <!-- film page 2 -->
</body>
</html>
2
  • 2
    $server-"/connectdb.php", is wrong, I think you want + instead of - Commented Mar 21, 2016 at 16:32
  • As @Stan points out, that line uses incorrect syntax - and a double forward slash (as your $server variable ends with one). You can use a relative path when sending an AJAX request. Commented Mar 21, 2016 at 16:39

1 Answer 1

1

I think this is where the issue lies

if(isset($_GET["include"])){

it should be

if(isset($_GET["action"]) && $_GET["action"]=="include"){

according to the ajax request parameters

Sign up to request clarification or add additional context in comments.

3 Comments

thanks i've changed that but it still hasn't made a difference
@usercmcl1712 also, you have a typo in $name = $('#$name').val(); where the html id has an extra $ sign, and in the ajax request, replace $server- with $server +
i didn't realize i had an extra $, have changed it but still isn't saving input to the database

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.