Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
jQuery Ajax post is not working

I know that there is a lot of questions like this out there, but I have been surfing them and other website for like 4 hours trying to figure this out. I am trying to get main.js to post the data via ajax and then the php should echo that data. If it does not work, it will echo "null". It keeps echoing "null" instead of "John". I know that the jquery and main.js links work, I have tested them. Here is main.js:

$(document).ready(function(){
    $.post("index.php", { test: "John"} );
});

And here is the php part of index.php:

<?php 
    $var = "null";
    if(isset($_POST['test'])) { 
    $var = $_POST['test'];
    }
    echo $var;
?>

I hope you can solve my problem, and thank you in advance.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Download HTTP Trace chrome extension, traceback ajax call and share a screenshot.

https://chrome.google.com/webstore/detail/http-trace/idladlllljmbcnfninpljlkaoklggknp

over 4 years ago · Santiago Trujillo Report

0

You are missing the callback function with the response from the server.

$.post( "index.php",{ test: "John"}, function( data ) {
  alert(data);
});

Or you can do something like this:

 $.post( "index.php",{ test: "John"})
  .done(function( data ) {
    alert( "Data Loaded: " + data );
  });

Please check the documentation Documentation

over 4 years ago · Santiago Trujillo Report

0

Give this a shot

jQuery

var available agent = 1;
jQuery.ajax({
    type: "POST",
    url: "your-url",
    data: available_agent,
    dataType: 'json',
    cache: false,
    contentType: false,
    processData: false,
    success:  function(data){
        owner = data['round_robin_agent'];
    },
    error : function () {
       alert("error");
    }
});

PHP Script

public function round_robin() {
    //Do your work
    $round_robin_agent = 'rob';
    $results_array = array(
        'round_robin_agent' => $round_robin_agent
    );
    return json_encode($results_array);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!