Submit form using ajax

后端 未结 2 1492
没有蜡笔的小新
没有蜡笔的小新 2020-12-22 09:07

I\'m trying to submit values to a database using jquery. I\'m new to ajax but I\'m required to do it with ajax.

This is what I\'ve done so far my php code is

相关标签:
2条回答
  • 2020-12-22 09:26

    Please reply to some of the comments so we can help figure out what the issues is. This is how your function would look with proper mysqli syntax.

    • How to make the connection: $mysql = new mysqli(...)

    • How to prepare the statement: $stmt = $mysqi->prepare(....)

    • How to bind the parameters: $stmt->bind_param(...)

    • Execution: $stmt->execute()

    Note: you were making your insert string like it was to be used for mysqli but your parameter binding was more similiar to pdo. I wasn't sure which one you were wanting to use so I chose mysqli. I personally prefer PDO, though... so I can re-write this using that if you want. Link to the PDO Prepare Function that also shows some binding.

     // this is your database connection for mysqli 
     $mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');
    
     function insertSeries($mysqli)
     {
        $options = array(
            'user' => $_POST['user'],
            'email' => $_POST['email'],
            'summary' => $_POST['summary'],
            'due_date' => $_POST['due_date'],
            'problem_type' => $_POST['problem_type'],
            'status' => $_POST['status']
        );
    
        $sql = "insert into ticket_summary('user','email','summary','due_date','problem_type','status') 
                                        Values (?,     ?,        ?,         ?,            ?,        ?)";
    
    
        if ( $stmt = $mysqli->prepare($sql) )
        { 
            $stmt->bind_param("ssssss", $options['user'], $options['email'],
                                         $options['summary'], $options['due_date'], 
                                         $options['problem_type'], $options['status']);
    
            $success = $stmt->execute();
    
            if ($success) { print "query worked"; }
                  else    { print "query failed"; }
        } 
     }
    
    0 讨论(0)
  • 2020-12-22 09:29

    Instead of interfering with the form's submit event, interfere with a click event instead. To make minimal changes to your existing setup, just add the click handler to the form submit button. First thing inside of the handler, call e.preventDefault(). You'll have to explicitly select the form inside the handler to serialize the data. Then, move your existing ajax call into that handler, and it should work fine. Also, make sure that you are actually calling insertSeries() somewhere in your PHP code.

    Cheers

    0 讨论(0)
提交回复
热议问题