Inserting into MySQL from PHP (jQuery/AJAX)

后端 未结 2 563
野趣味
野趣味 2020-11-29 05:32

I have seen many tutorials, but they\'re so confusing, and to do what I want to do, I just don\'t get how to use existing stuff from those tutorials and make them work they

2条回答
  •  执念已碎
    2020-11-29 06:05

    The jQuery part is often quite simple. It just redirects the ordinary form action= over a Javascript handler. $.post is easy to use and you just need .serialize() to package up the existing form values into a string:

    And on PHP side you simply receive the content via $_POST and save it to the database (using the old mysql_ functions would also be possible, just more cumbersome):

    $db = new PDO("mysql:...");
    
    if ($_POST["submitbuttonname"]) {
    
       $q = $db->prepare("INSERT INTO save (textbox, label) VALUES (?, ?)";
       $q->execute(array($_POST["textbox"], $_POST["label"]));
    

提交回复
热议问题