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!

David Mendienta
  • 395
  • 2
  • 5
  • 11
  • How is the `sendMessage` function being called? Did you look for a response in the browser developer tools? – Mottie Jan 07 '17 at 04:06
  • did you check with using error call back ? if the call is failing – Deep Jan 07 '17 at 04:07
  • 1
    i suggest you to add an error callback after success to check if the request is failing at some point. – Farooq Ahmed Khan Jan 07 '17 at 04:08
  • Try setting the​ content type of the header of the response to json ` header('Content-type: application/json');` – Nadir Laskar Jan 07 '17 at 04:11
  • check browser developer tools network tab to see what is actually happening regarding request and response – Jaromanda X Jan 07 '17 at 04:12
  • The fact that this php file is somewhere inside the wordpress directory doesn't matter one bit; you need to actually include some wordpress php code before you can use its variables and functions. –  Jan 07 '17 at 04:50

1 Answers1

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?

Community
  • 1
  • 1
Pawan Developers
  • 377
  • 4
  • 16