dtrotfd1012 2013-02-03 15:58
浏览 100
已采纳

PHP / Javascript - 通过POST将日期字符串传递给AJAX无法正常工作

I am creating a simple form that is to post data to a MySQL. Your thinking why POST and not GET? well there is a description field in this form that allows for many characters, and given that GET has its character limitations I opted to use POST.

Without further adieu, Here is my HTML form.

<form name="addExperienceForm" id="addExperienceForm" style="display:none;">
                    Title:<input type="text" name="title" id="title" />
                    From:<input type="text" name="startDate" id="startDate" />
                    To:<input type="text" name="endDate" id="endDate" />
                    Description:<textarea type="message" name="description" id="description"></textarea>
                    <input type="button" value="Submit" onclick="addUserExp()"/>
 </form>

here is the jQuery Datepicker code for the date boxes in the form:

//allow a date range to be selected
        $("#startDate").datepicker({
                    changeMonth : true,
                    changeYear : true,
                    dateFormat : "M,yy",
        })
        $("#endDate").datepicker({
                    changeMonth : true,
                    changeYear : true, 
                    dateFormat : "M,yy",
        })  

This is the AJAX function call:

function addUserExp(){
        var title = document.getElementById('title').value;
        var startDate = document.getElementById('startDate').value;
        var endDate = document.getElementById('endDate').value;
        var description = document.getElementById('description').value;
        var str = "title="+title+"start="+startDate+"end="+endDate+"desc="+description;
        var req = getXMLHTTP();

        if(req){

            req.onreadystatechange = function(){
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('addNewExp').innerHTML=req.responseText;    


                    } else {
                        alert("There was a problem while using XMLHTTP:
" + req.statusText);
                    }
                }
            }
        }
        req.open("post", "addExperience.php", true);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send(str);
    }

I suspect the problem is in the AJAX call, possibly something to do with my 'str' variable.

  • 写回答

3条回答 默认 最新

  • douxing1969 2013-02-03 16:42
    关注

    I haven't tested your code, but shouldn't str be:

     var str = "title="+title+"&start="+startDate+"&end="+endDate+"&desc="+description;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装