PHP $_POST not working as expected

时间:2016-10-20 19:45:58

标签: javascript php jquery

In one of my form i have a dropdownlist control on whose "onchange" event i am populating two textbox controls as shown below:

function pop_mli_n_murl(){
 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);
          }
    });
  }

Up until here everything works fine. However when i submit the form, $_POST['txtMLI'] and $_POST['txtMLIURL'] is not set while the others are. Can you tell what could be the problem??

2 个答案:

答案 0 :(得分:0)

You probably forgot to prevent the NORMAL form submission, so your ajax request gets terminated. If the ajax call was succeeding, the ONLY form value you should ever receive would be $_POST['wdpid'], because that's the only thing your ajax code tries to send.

Getting the OTHER form values means the normal form submission occurred, NOT the ajax call.

That means you need something like

<form ... onsubmit="return pop_mli_n_murl()">
...

function pop_mi_n_murl() {
    ... ajax stuff ...
    return false;  // return false to onsubmit handler, terminating the normal submit
}

答案 1 :(得分:0)

When you submit the ajax you're not posting txtMLI or xtMLIURL, you're only sending wdpid.

The data in the ajax should be something like

{
    'wdpid': 'value here',
    'txtMLI':  'value here',
    'xtMLIURL': 'value here',
}

Then on the server side you'll be able to access those variables from the ldd_pop_wd_pdata.php script.

相关问题