$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - My jQuery Ajax form submit is still refreshing page?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - My jQuery Ajax form submit is still refreshing page?

matteradmin8PV0评论

I managed to get my Wordpress jQuery wired properly. Everything gets inserted to the database as expected, but for some reason, it is still refreshing the whole page?

php/html (Wordpress Page Template):

<?php
if(isset($_POST['send']))
{
   send_projectmessage($projectid, $userid, $projectmessage);
}
?>
<html>
<body>
<div id="messages">
<?php 
$messages = load_projectmessages($projectid);
        foreach ($messages as $message) { /*echo the messages and put in a nice div*/ }
?>
</div>
<form id="form-pm" method="post" enctype="multipart/form-data" action="">
            <textarea name="projectMessage" rows=3 id="project-message"></textarea>

            <input type="submit" name="send" value="send message">
</form>

JS:

$('#form-pm').on('send', function(e) {

        e.preventDefault();

        var ajaxRequest =
        $.ajax({
            url: admin_ajax.ajax_url,
            type: 'post',
            data: { 
                action: 'send_projectmessage',
                projectid: 'projectid',
                userid: 'userid',
                projectmessage: 'projectmessage'
                }, 
            contentType: 'application/json; charset=utf-8',
            dataType: 'json' });

        ajaxRequest.done(function() { } );
        ajaxRequest.fail(function(jqXHR) { alert('You are fail. ' + jqXHR); } );

        return false;
    });

functions.php

function send_projectmessage($projectid, $userid, $message) {
    global $wpdb;
    $wpdb->insert('tbl_messages', array(
        'project_id' => $projectid,
        'user_id' => $userid,
        'message_body' => $message
    ));
    return false;
    wp_die();
}
add_action('wp_ajax_send_projectmessage', 'send_projectmessage');

I have the e.preventDefault(); and also return false. My message gets inserted properly into the MySql table, but the whole page still reloads!

I managed to get my Wordpress jQuery wired properly. Everything gets inserted to the database as expected, but for some reason, it is still refreshing the whole page?

php/html (Wordpress Page Template):

<?php
if(isset($_POST['send']))
{
   send_projectmessage($projectid, $userid, $projectmessage);
}
?>
<html>
<body>
<div id="messages">
<?php 
$messages = load_projectmessages($projectid);
        foreach ($messages as $message) { /*echo the messages and put in a nice div*/ }
?>
</div>
<form id="form-pm" method="post" enctype="multipart/form-data" action="">
            <textarea name="projectMessage" rows=3 id="project-message"></textarea>

            <input type="submit" name="send" value="send message">
</form>

JS:

$('#form-pm').on('send', function(e) {

        e.preventDefault();

        var ajaxRequest =
        $.ajax({
            url: admin_ajax.ajax_url,
            type: 'post',
            data: { 
                action: 'send_projectmessage',
                projectid: 'projectid',
                userid: 'userid',
                projectmessage: 'projectmessage'
                }, 
            contentType: 'application/json; charset=utf-8',
            dataType: 'json' });

        ajaxRequest.done(function() { } );
        ajaxRequest.fail(function(jqXHR) { alert('You are fail. ' + jqXHR); } );

        return false;
    });

functions.php

function send_projectmessage($projectid, $userid, $message) {
    global $wpdb;
    $wpdb->insert('tbl_messages', array(
        'project_id' => $projectid,
        'user_id' => $userid,
        'message_body' => $message
    ));
    return false;
    wp_die();
}
add_action('wp_ajax_send_projectmessage', 'send_projectmessage');

I have the e.preventDefault(); and also return false. My message gets inserted properly into the MySql table, but the whole page still reloads!

Share Improve this question asked Feb 20, 2019 at 18:20 RollorRollor 1291 gold badge4 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You use wrong js event. You register your callback with this code:

$('#form-pm').on('send', function(e) {

But there is no event called send on form. It should be submit. Here’s fixed version of that line:

$('#form-pm').on('submit', function(e) {
Post a comment

comment list (0)

  1. No comments so far