简体   繁体   中英

How do I make ajax interact with a database in order to post, get, and delete?

How do I use ajax to post/delete/get to and from a database?

I want to be able to post and be able to delete whatever I post to this link:

http://www.bmoseley.com/ajax/listrecords.php

(this is an assignment)

however, I have to use /ajax/addrecord.php and /ajax/deleterecord.php in order to be able to add and delete posts to /ajax/lisrecords.php

Let me state this: I don't want you to assume that you're doing an assignment for me, I want someone to explain how wrong I am with my code and understanding of ajax and what ajax script I can use in order to accomplish my goal.

This is my code:

HTML:

<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css">
<script src="http://code.jquery.com/jquery-latest.min.js"
    type="text/javascript"></script>
<script>
 function validateForm ()
{
var x=document.forms["myForm"] ["fullname"].value;
if (x==null || x=="")
    {
    alert ("First name must be filled out");
    return false;
    }
}

    function isNumberKey(evt) {
        var e = evt || window.event; //window.event is safer,
    var charCode = e.which || e.keyCode;                        
    if (charCode > 31 && (charCode < 47 || charCode > 57))
    return false;
    if (e.shiftKey) return false;
    return true;
}
</script>
 </head>
<body>
<div id="wrap">
<div id="wrapper">
<div id="info">
<form name="myForm" onsubmit="return validateForm()" method="get" action="http://www.bmoseley.com/ajax/listrecords.php">
<table border="0">
<tr>
<td><input class="fullname" maxlength="50" type="text" name="fname" placeholder="Name</td>
</tr>
<tr>
<td><input class="phonenumber" maxlength="10" onkeypress="return isNumberKey(event)" placeholder="Phone Number"></td>
</tr>
<tr>
<td><input class="button" type="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
<div id="displayInfo">
<script>
$.ajax({
type: 'GET',
data: JSON.stringify(foo),
url: "http://www.bmoseley.com/ajax/listrecords.php",
success: function(data){console.log(data);}, 
failure: function(e){console.log('ERROR: ' + e)}
});
</script>
</div>
</div>
</div>
</body>
</html>

your form is directly sending to your php-file, you must prevent it to submit to your php, but to send via your ajax request:

<!DOCTYPE html>
<html lang="en-us">
    <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="style.css" type="text/css">
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script>
         function validateForm () {
             var x=document.forms["myForm"] ["fullname"].value;
            if (x==null || x=="")
            {
                alert ("First name must be filled out");
                return false;
            }
        }

        function isNumberKey(evt) {
            var e = evt || window.event; //window.event is safer,
            var charCode = e.which || e.keyCode;
            if (charCode > 31 && (charCode < 47 || charCode > 57))
                return false;
            if (e.shiftKey) return false;
            return true;
        }
        </script>
     </head>
<body>
    <div id="wrap">
        <div id="wrapper">
            <div id="info">
                <form id="myForm" method="get" action="/ajax/listrecords.php">
                    <table border="0">
                        <tr>
                            <td><input class="fullname" maxlength="50" type="text" name="fname" placeholder="Name</td>
                        </tr>
                        <tr>
                            <td><input class="phonenumber" maxlength="10" onkeypress="return isNumberKey(event)" placeholder="Phone Number"></td>
                        </tr>
                        <tr>
                            <td><input class="button" type="submit" value="Submit"></td>
                        </tr>
                    </table>
                </form>
                <div id="displayInfo">
                </div>
                <script>
                        //loaded when dom is ready
                        $(function() {
                            var frm = $('#myForm');
                            //submit is nearly the same as onsubmit in your html before
                            frm.submit(function (ev) {
                                //preventDefault prevents the form to submit normally, because you dont want this, you want ajax!
                                ev.preventDefault();

                                //validating your form
                                if(validateForm())
                                {
                                    //if valide make the request
                                    $.ajax({
                                        type: frm.attr('method'),
                                        url: frm.attr('action'),
                                        data: frm.serialize(),
                                        success: function (data) {
                                            alert('ok');
                                            //or when data is only a string you could add it to your div
                                            $('#displayInfo').html(data);
                                        }
                                    });
                                }

                            });
                        });
                </script>
            </div>
        </div>
    </div>
</body>
</html>

in the ajax-jquery-docs you will find more examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM