我试图使用Ajax将数据发送到另一个页面

时间:2018-04-17 10:15:48

标签: javascript php jquery html

好的,所以它有点复杂,我不明白为什么这不起作用。我试图创建一个页面,其中有一些文本框和Dropbox列表,以及一个使用easyui的数据网格(该部分运行良好)。我想要做的是,当我在一个保管箱列表中选择某些内容时,我做了两个动作:将一些信息发送到另一个php页面以写入数据库,并更改我的数据网格的列。这几乎可行。我在数据库中写入,但每个值都为null,我的列也会更改。

所以,我对我的droplist的内容是:



    function changecolumn(){
    //those are my data from textbox/dropbox list
    		var headerData = {
    			'date'      : $('input[name=date]').val(),
    			'sunset'    : document.getElementById('sunset').value,
    			'sunrise'    : $('input[name=sunrise]').val(),
    			'moonset'    : $('input[name=moonset]').val(),
    			'moonrise'    : $('input[name=moonrise]').val(),
    			'moonphase'    : $('input[name=moonphase]').val(),
    			'instrument'    : selectedInstrument,
    			'obsname'    : $('input[name=obsname]').val(),
    			'astroresp'    : $('input[name=astroresp]').val(),
    			'techname'    : $('input[name=techname]').val()
    		};
        
    //Trying to uses ajax here.    
    		$.ajax({  
                              url:"test.php",  
                              method:"POST",
                              data: "headerData",
                              //data: {sunset : document.getElementById('sunset').value, sunrise : document.getElementById('sunrise').value, moonset : document.getElementById('moonset').value, moonrise : document.getElementById('moonrise').value, moonphase : document.getElementById('moonphase').value, instrument : selectedInstrument, obsname : document.getElementById('obsname').value, astroresp : document.getElementById('astroresp').value, techname : document.getElementById('techname').value  },
    			   success: function(data) {
                    alert("Data was succesfully captured"); },
            })
                // using the done promise callback
                .done(function(data) {

                    // log data to the console so we can see
                    console.log(data); 

                    // here we will handle errors and validation messages
                });


        //THIS PART IS WORKING, FOR MY DATAGRID
    		var listInstrument = document.getElementById("instrument");
    		var selectedInstrument = listInstrument.options[listInstrument.selectedIndex].text;
    		
    		if(selectedInstrument == 'one'){
    			$('#dg').datagrid({
    				columns:[[
            //this part is working
    				]]
    			});
    		}
    		else { 
        //this part is working
    		}
    		
    	}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="instrument" name="instrument" onchange="changecolumn()">
    <option>
    <option> one
    <option> two
    <option> three
</select>
&#13;
&#13;
&#13;

在我的PHP页面中,我使用:$ sunrise = $ _POST [&#39; sunrise&#39;];尝试获取数据,但一切都是空的..

我不确定出了什么问题,任何人都有任何想法?

2 个答案:

答案 0 :(得分:2)

您使用data: "headerData"ajax来电中将数据设置为字符串,您需要更改如下:

var headerData = {
        date      : $('input[name="date"]').val(),
        sunset    : document.getElementById("sunset").value,
        sunrise    : $('input[name="sunrise"]').val(),//add " in the query
        moonset    : $('input[name="moonset"]').val(),
        moonrise    : $('input[name="moonrise"]').val(),
        moonphase    : $('input[name="moonphase"]').val(),
        instrument    : selectedInstrument,
        obsname    : $('input[name="obsname"]').val(),
        astroresp    : $('input[name="astroresp"]').val(),
        techname    : $('input[name="techname"]').val()
};
//console.log(headerData); //check if it has data or not

$.ajax({  
          url:"test.php",  
          type:"POST",
          data: headerData,                
          success: function(data) {
                alert("Data was succesfully captured"); 
          },
}).done(function(data){

});

答案 1 :(得分:1)

当您以json格式传递数据时,需要添加&#34; dataType:json&#34;

$.ajax({  
          url:"test.php",  
          method:"POST",
          data: headerData,
          dataType: 'json',                
          success: function(data) {
                alert("Data was succesfully captured"); 
          },
})