0

Updated- Added the insert code to save.php, but clicking on Save, no action is happening. Fields values are not getting inserted to db and also I am not getting the alert. Please advice.

I am trying to insert few user inputs to my mysql database using Ajax. Here is my html page:

<form id="form1" action="#" method="post">
            <table style="padding-top: 25px;">
                <tr style="height: 40px;">
                    <td style="text-align:right;">field1:</td>
                    <td><input type="text" id="field1" required/></td>
                </tr>
                <tr style="height: 40px;">
                    <td style="text-align:right;">field2:</td>
                    <td style="width: 286px;"><input type="text" id="field2"/>    </td>
                </tr>
                <tr style="height: 40px;">
                    <td style="text-align:right;">Field3</td>
                    <td><input type="text" id="field3"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td style="padding-top: 20px;">
                        <input type="submit" value="Save" id="Save"/>
                    </td>
                </tr>
           </table>
</form>

Here is my Ajax:

<script>
        $(document).ready(function() {          
            $('#Save').click(function() {   
                var field1 = $('#field1').val();
                var field2 = $('#field2').val();
                var field3 = $('#field3').val();

                $.ajax({
                    type: 'POST',
                    async: false,        
                    url: 'save.php',
                    data: {
                            'saverecord': 1,
                            'field1':field1, 
                            'field2':field2, 
                            'field3':field3,
                    success: function(data) 
                    {
                        if(data==0){
                                $('#field1').val('');
                                $('#field2').val('');
                                $('#field3').val('');       
                        }
                    }
                });
            });
        });
    </script>

In my save.php file, I am trying to connect to my mysql database like this:

<?php
 $cn = mysql_connect("localhost","root","");    
 if($cn) 
 {
    mysql_select_db('databasename', $cn);
 }
 if(isset($_POST['saverecord']))
    {
        mysql_query("INSERT INTO table (field1, field2, field3) VALUES
      ('{$_POST['field1']}','{$_POST['field2']}','{$_POST['field3']}')");
        alert("Inserted successfully");
        exit();
    }
?>

Please let me know how I can insert the above mentioned 3 field values into my table using Ajax and html? Thank you so much.

Katie
  • 763
  • 1
  • 9
  • 21

4 Answers4

1

Put the insert in save.php, after the connection code. Your PHP code has to be in a .php file, or your server must be configured to parse .htm/.html files as PHP code. Otherwise, it is not processed and is sent to the browser as HTML, PHP code and all, and the browser sees <?php ... ?> as simply a weird HTML tag.

Also, please don't use mysql_*; the mysql_* functions are outdated, deprecated, and insecure. Use MySQLi or PDO instead.

On that note, you are wide open to SQL injection. You must use prepared statements or at least properly escape data before adding it to a SQL query or you will get hacked.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
0

The query should be like :

"INSERT INTO table (field1, field2, field3) VALUES ('".$_POST['field1']."','".$_POST['field2']."','".$_POST['field3']."')"

This must be in a .php file that your ajax is pointing i.e, save.php

Also please avoid using the deprecated mysql functions and move to either mysqli

AnkiiG
  • 3,468
  • 1
  • 17
  • 28
  • _I am not sure where or how to add the insert queries in html. I tried adding like below in my html page_ Are you sure this would help? – DirtyBit Sep 27 '15 at 18:23
  • 1
    I understand that you have the best of intentions to help out, but the way he writes his query technically work. I would suggest that you eloborate your answer so that he can solve the issue he has. And, when you suggest using mysqli_* or PDO (which you do and should do) then write your examples using prepared statements accordingly. – faerin Sep 27 '15 at 18:30
0

insert Query will be place in your save.php file

For your connection

<?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

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


    "INSERT INTO table (field1, field2, field3) VALUES
          ('".$_POST['field1']."','".$_POST['field2']."','".$_POST['field3']."')"
HEMANT SUMAN
  • 478
  • 2
  • 13
0

Your query should be in the save.php file that you post to. Basically combine the last 2 code blocks into save.php.

the
  • 21,007
  • 11
  • 68
  • 101
Traveling_Monk
  • 778
  • 2
  • 11
  • 28