0

I have a problem getting my form to insert the records into the database. I just can't figure out where I'm wrong...

My form is bellow

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="script.js"></script>

<form id="form" name="form">
    <div>
        <label>Name :</label>
        <input id="name" type="text">
        <label>Email :</label>
        <input id="submit" onclick="myFunction()" type="button" value="Submit">
    </div>
</form>

a script script.js

function myFunction() {
    var name = document.getElementById("name").value;
    var dataString = 'name1=' + name;
    if (name == '') {
        alert("Please Fill All Fields");
    } else {
        $.ajax({
            type: "POST",
            url: "ajaxjs.php",
            data: dataString,
            cache: false,
            success: function(html) {
                alert(html);
            }
        });
    }
    return false;
}

and a php script ajaxjs.php

<?php
include ('./includes/connection.php');
$name1 = $_POST['name1'];
if (isset($_POST['name1'])) {
    $query = mysql_query("INSERT INTO db VALUES('TEST','name1')");
}
?>

developer tools show message: script.js:5 Uncaught ReferenceError: dataString is not definedmyFunction @ script.js:5onclick @ test.html:9

3
  • 1
    Turn on developer tools in your browser and see the error your getting update your question with the error Commented Jul 16, 2016 at 18:08
  • @Danturnip script.js:5 Uncaught ReferenceError: dataString is not definedmyFunction @ script.js:5onclick @ test.html:9 Commented Jul 16, 2016 at 18:14
  • Sidenote: even if it works without errors, you'd be inserting a constant string (name1) instead of the posted value. Commented Jul 16, 2016 at 18:16

1 Answer 1

2

Your Ajax code working well But your query command look like wrong Please try

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

enter image description here

Update: Source code working well

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    function myFunction() {
        var name = document.getElementById("name").value;
        var dataString = 'name1=' + name;
        if (name == '') {
            alert("Please Fill All Fields");
        } else {
            $.ajax({
                type: "POST",
                url: "ajaxjs.php",
                data: dataString,
                cache: false,
                success: function(html) {
                alert(html);
                }
            });
        }
        return false;
    }
</script>
<form id="form" name="form">
<div>
<label>Name :</label>
<input id="name" type="text">
<label>Email :</label>
<input id="submit" onclick="myFunction()" type="button" value="Submit">
</div>
</form>

And ajaxjs.php

<?php
    // include ('./includes/connection.php');
    $name1 = $_POST['name1'];
    echo $name1;
    // if (isset($_POST['name1'])) {
    // $query = mysql_query("INSERT INTO db VALUES('TEST','name1')");
    // }
?>
Sign up to request clarification or add additional context in comments.

6 Comments

You can omit the column names if you provide a value for each table column. Reference: If you do not specify a list of column names for INSERT ... VALUES [..], values for every column in the table must be provided by the VALUES [...]
I think his query not working and I give one example.
Yes bro, I know it. xD I just give one full example xD
@Menel Hardly... I did nothing xD
Ah soz, I mean Quynh
|

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.