Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:38:43+00:00 2026-06-13T17:38:43+00:00

How do I use AJAX to return a variable in PHP? I am currently

  • 0

How do I use AJAX to return a variable in PHP? I am currently using echo in my controller to display a price on dropdown .change in a div called price.

However I have a hidden field which I need to return the row id to on change. How do I assign the return var in jQuery so that I can echo it in my hidden field?

jQuery

$(document).ready(function() {
    $('#pricingEngine').change(function() {
         var query = $("#pricingEngine").serialize();
         $('#price').fadeOut(500).addClass('ajax-loading');
         $.ajax({
             type: "POST",
             url: "store/PricingEngine",
             data: query,
             success: function(data)
             {
                  $('#price').removeClass('ajax-loading').html('$' + data).fadeIn(500);
             }
         });
    return false;
   });

});

Controller

function PricingEngine()
{
    //print_r($_POST);
    $this->load->model('M_Pricing');
    $post_options = array(
      'X_SIZE' => $this->input->post('X_SIZE'),
      'X_PAPER' => $this->input->post('X_PAPER'),
      'X_COLOR' => $this->input->post('X_COLOR'),
      'X_QTY' => $this->input->post('X_QTY'),
      'O_RC' => $this->input->post('O_RC')
                          );

    $data = $this->M_Pricing->ajax_price_engine($post_options);

    foreach($data as $pData) {
        echo number_format($pData->F_PRICE / 1000,2);
        return $ProductById = $pData->businesscards_id;
    }
}

View

Here is my hidden field I want to pass the VAR to every-time the form is changed.
” />

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:38:44+00:00Added an answer on June 13, 2026 at 5:38 pm

    Well.. One option would be to return a JSON object. To create a JSON object in PHP, you start with an array of values and you execute json_encode($arr). This will return a JSON string.

    $arr = array(
      'stack'=>'overflow',
      'key'=>'value'
    );
    echo json_encode($arr);
    
    {"stack":"overflow","key":"value"}
    

    Now in your jQuery, you’ll have to tell your $.ajax call that you are expecting some JSON return values, so you specify another parameter – dataType : 'json'. Now your returned values in the success function will be a normal JavaScript object.

    $.ajax({
      type: "POST",
      url: "...",
      data: query,
      dataType: 'json',
      success: function(data){
        console.log(data.stack); // overflow
        console.log(data.key);   // value
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a contact form i want to use Ajax for. My contact.php script
I am using jQuery to call a php page (via ajax) that goes to
I am retrieving data from database using AJAX and PHP. In database one of
I use this AJAX code: <script language=javascript type=text/javascript> <!-- //Browser Support Code function ajaxFunction(){
I am trying to use jQuery to make a synchronous AJAX post to a
I'm trying to use jQuery's get function to call my php script. The php
I want to get the value of a single variable from my Controller class
I'm calling a php function from my $.ajax call to add a new item
right now , I use jQUery(document).ready to inject javascript into a div. All is
I'm writing a PHP application which uses AJAX to submit forms via POST when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.