1

Thanks for taking a look at my question, firstly, I know this is not the first time a question of this nature has been asked, but I have being reading StackOverflow for over 3 hours now... still can't figure it out.

Here's the gist:

I'm trying to send a value from messaging.php to messaging.js using json_encode.

-> Here's the messaging.php code:

<?php
header('Content-Type: application/json');
global $wpdb;
$current_user = wp_get_current_user();

$to = $_POST['uname'];
$subject = $_POST['subject'];
$message = $_POST['msg'];


$table_name = $wpdb->prefix . 'none_of_ur_business';

if(isset($to) && isset($to) && isset($to)):
$wpdb->insert(
    $table_name,
    array(
        'notit_sender_userid' => $current_user->display_name,
        'notit_receiver_userid' => $to,
        'notit_subject' => $subject,
        'notit_message' => $messagem
    )
);

$testtext = 'does this work??';
echo json_encode(array('test' => $testtext));


endif;

Here's the messaging.js code:

function sendMessage(uname, subject, message){
  $.ajax({
    url : wp_directory+'/modules/messaging/messaging.php',
    dataType : 'JSON',
    type : 'post',
    data: {
         'uname' : uname,
         'subject' : subject,
         'msg' : message
     },
     success: function(data) {
         alert(data.test);
     }

  });

A couple of relevant things:

  • I am developing on the WordPress platform
  • I use messaging.php to send data to my database (Maybe that's why, it's not working??)

I don't get anything from the ajax success function, it never "alerts"

Please provide any help you can, I would highly appreciate it!

6
  • How is the sendMessage function being called? Did you look for a response in the browser developer tools? Commented Jan 7, 2017 at 4:06
  • did you check with using error call back ? if the call is failing Commented Jan 7, 2017 at 4:07
  • 1
    i suggest you to add an error callback after success to check if the request is failing at some point. Commented Jan 7, 2017 at 4:08
  • Try setting the​ content type of the header of the response to json ` header('Content-type: application/json');` Commented Jan 7, 2017 at 4:11
  • check browser developer tools network tab to see what is actually happening regarding request and response Commented Jan 7, 2017 at 4:12

1 Answer 1

3

in wordpress you are trying to access file (messaging.php) separatly( out of wordpress) and in messaing.php you have used " global $wpdb; " which is wrong.

First you should include necessary wordpress files. Add below code to your messaging.php

define( 'SHORTINIT', true );
require '{wp_root}/wp-load.php';

change {wp_root}. if your wordpress installed on server root then it will be like $_SERVER["DOCUMET_ROOT"] or you have to adjust manually.

for more info check this page: Using WPDB in standalone script?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.