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条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题