0

a form is not working correctly with ajax jquery after adding it into a div, the id's are not getting sent/posted to the jquery function so the message doesnt have a content and receiver id.

i press the users button:

<jquery-form formmethod='post'>
            <input type="submit" onclick="js_vis_melding_liste(<?php echo $tempid; ?>)" value="<?php echo $userbutton->first_name . ' ' . $userbutton->last_name; ?>"/><br/>
        </jquery-form>

starting the js function:

function js_vis_melding_liste(motid) {
    jQuery.ajax({
        url: my_ajax_script.ajaxurl,
        type: 'POST',
        data: ({action: 'liste_meldinger', mottaid: motid}), //utfører liste_meldinger funksjonen og mulighet for å hente mottaid via $_POST
        success: function(tingting) {
            jQuery('#ham-meldinginnboks').html(tingting); //laster inn resulatet av liste_meldinger funksjonen inn i div
            jQuery('#ikkevalgtmottakerdiv').hide(); //skjulerdiven
            jQuery('#ham-meldinginnboks').show();
            var objDiv = document.getElementById("meldinglisten1"); //denne og neste linje sørger for at scrollbar er nederst
            objDiv.scrollTop = objDiv.scrollHeight;
        }
    });
}
;

which uses the php function in functions.php:

function liste_meldinger() {

    $mottakid = $_POST['mottaid'];
    global $current_user, $wpdb, $valgt_mottaker;
    $mottaker = get_user_by('id', $mottakid);

    $valgt_mottaker = $mottakid;
    $curUserId = get_current_user_id();
    $meldingliste =
            $wpdb->get_results("
        SELECT c.inn AS innh, mottakerid, datom
FROM
(
    SELECT innhold AS inn, datom, mottakerid
    FROM meldinger
    WHERE mottakerid = '$curUserId' and senderid = '$mottakid'

    UNION

    SELECT innhold AS inn, datom, mottakerid
    FROM meldinger
    WHERE senderid = '$curUserId' and mottakerid = '$mottakid'
) AS c
ORDER BY datom ASC", ARRAY_A);
    $sendinfo = "<p>Liste over meldinger mellom deg og $mottaker->first_name $mottaker->last_name.</p><br/>
            <div id='meldinglisten1' style='overflow-y: auto;height: 400px;'>";

    foreach ($meldingliste as $meldinger):

        $melding = $meldinger['innh'];
        $datomo = $meldinger['datom'];
        if ($meldinger['mottakerid'] == $curUserId) {
            $sendinfo .= $datomo . ' - ' . $mottaker->first_name . ' ' . $mottaker->last_name . ' sa:';
        } else {
            $sendinfo .= $datomo . ' - ' . $current_user->first_name . ' ' . $current_user->last_name . ' sa:';
        }


        $sendinfo .= "<div style='border:1px solid #dedede'><p>";
        $sendinfo .= $melding;
        $sendinfo .= "</p>
        </div>
        <br/>";
    endforeach;
    $sendinfo .= '</div>

    <div>
        <jquery-form name="sendersvarform" id="sendersvarform" onsubmit="js_svar_melding();" >
            Skriv ny melding til ' . $mottaker->first_name . ' :<br/>
            <textarea name="meldinginnhold" id="meldinginnhold" rows="3" cols="20"></textarea> <br/>
            <input type="hidden" name="idmot" id="idmot" value="' . $mottakid . '" />
                <input  type="submit" name="submit" id="svarmelding" value="Send Svar"/>
        </jquery-form>
    </div>';

    echo $sendinfo;
    die();
}

and it shows messages and a reply box, which is the id=sendersvarform pressing reply, it uses the js_svar_melding function:

function js_svar_melding() {
    var arr = jQuery('#sendersvarform').serialize();
    arr += '&action=svar_melding';
    jQuery.ajax({
        url: my_ajax_script.ajaxurl,
        type: 'POST',
        data: arr,
        success: function(mld) {
            alert(mld);
        }
    });
    return false;
}
;

which again uses the php function svar_melding:

function svar_melding() {
    global $wpdb, $current_user;

    $userID = $current_user->ID;
    if (!empty($_POST['idmot']) && !empty($_POST['meldinginnhold'])) {
        $sid = $userID;
        $mid = $_POST['idmot'];
        $mldinnhold = $_POST['meldinginnhold'];

        ham_sendmelding($sid, $mid, $mldinnhold);
        echo "melding sendt.";
    } else {
        echo "Ikke alle feltene er fylt inn.";
    }
    die();
}

it seems like the $_POST['idmot'] and $_POST['meldinginnhold'] are empty since if i do not use the if !empty then it will create a message but it has not id or content, it should have posted the information but it doesnt, and it reloads page back to Home which it should not because of the return false in js_svar_melding, please help

4

1 回答 1

0

找到了我猜的答案。通过使用 .on

jQuery("#sendersvarform").on("submit", false);

它将“激活”表单 #sendersvarform 提交时发生的事件。创建表单后激活它。

function js_vis_melding_liste(motid) {
    jQuery.ajax({
        url: my_ajax_script.ajaxurl,
        type: 'POST',
        data: ({action: 'liste_meldinger', mottaid: motid}), //utfører liste_meldinger funksjonen og mulighet for å hente mottaid via $_POST
        success: function(tingting) {
            jQuery('#ham-meldinginnboks').html(tingting); //laster inn resulatet av liste_meldinger funksjonen inn i div
            jQuery('#ikkevalgtmottakerdiv').hide(); //skjuler diven
            jQuery("#sendersvarform").on("submit", false);
            jQuery('#ham-meldinginnboks').show();
            var objDiv = document.getElementById("meldinglisten1"); //denne og neste linje sørger for at scrollbar er nederst
            objDiv.scrollTop = objDiv.scrollHeight;
        }
    });
}
;
于 2013-05-26T18:59:10.220 回答