-3

I have a simple PHP form containing two (2) textboxes a dropdownlist and finally a SUBMIT button. In this form, before hitting the Submit button, the user has to select an item from the dropdown list. Upon selection of an item from the dropdown list, it's onchange event fires and populates the other 2 textboxes with some data fetched from the database.

The code to fetch data:

var wd_pid = document.getElementById("ddlUnder").value;
var dataString = 'wdpid='+ wd_pid;
   $.ajax({
            type: "POST",
            url: "ldd_pop_wd_pdata.php",
            data: dataString,
            cache: false,
            success: function(result){
                        var v1= result.substring(0,result.indexOf('='));
                        var v2= result.substring(result.indexOf('=')+1);
                               $("#txtMLI").val(v1);
                               $("#txtMLIURL").val(v2);
                      }
         });

The above code works perfectly, populating the two textboxes with correct data. This forms 'action' attribute points to an another php page containing logic to save form data into the database. My problem begins in the next page. After form submit, in the next php page, the post variables $_POST['txtMLI'] & $_POST['txtMLIURL'] are empty.Why is it so?? Please advise what's going wrong.

Thanks in advance.

5
  • 1
    you need to send data in this manner data: {dataString:dataString} Commented Oct 21, 2016 at 13:06
  • 1
    Show the form as well. Commented Oct 21, 2016 at 13:06
  • 4
    please provide a minimal reproducible example Commented Oct 21, 2016 at 13:06
  • POST variables are not sessions, they will only exist in the context of the page they where loaded on. If you want to keep a variable stored through many pages you will want to use SESSIONS. Also values are stored based on name attributes and not id's. Commented Oct 21, 2016 at 13:09
  • Are you sure the data is comming into the textboxes?. I see you are doing a POST with ajax using a querystring instead of a GET to fetch it. Debug this call on the console, and then if the data is there, then post the code for the submit action you do afterwards. Check this for reference on how to your DDL ajax call: stackoverflow.com/questions/22384647/… Commented Oct 21, 2016 at 13:17

1 Answer 1

0

The data part of your AJAX call is not properly formatted. It is supposed to be formatted in JSON as follows.

{ wdpid: wd_pid }

And not

"wdpid=" + wd_pid
Sign up to request clarification or add additional context in comments.

1 Comment

It can accept strings, if JSON was given it would need to be parsed.

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.