dqudtskm49788 2016-05-05 05:54
浏览 58
已采纳

使用ajax时php中未定义的索引

My Form page :

<body>  
<head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script>
        function val()
        {
            var name=document.getElementById("name").value;
            if(name == '')
            {
                alert(name+" is empty");
            }
            else
            {
            var dataString = "name = "+name;
            $.ajax({
                type:"POST",
                url:"hi.php",
                data:dataString,
                cache:false,
                success:function(html){
                    $('#msg').html(html);
                }
            });
            }
        return false;
        }
        </script>

    </head>
<body>
    <form>
        <input type="text" id="name" >
        <br/><br/>
        <input type="submit" value ="submit" onclick="return val()">
    </form>
<p id="msg"></p>
</body>

Here is my hi.php file

<?php
$name = $_POST["name"];
echo "Response : ".$name;
?>

When clicking on submit button it show an error Notice: Undefined index: name in C:\wamp\www\SendEmailAjaxJquery\hi.php on line 2

I don't know where is the error plz help me to find out the error...

Thanks in advance

  • 写回答

3条回答 默认 最新

  • doy51723 2016-05-05 06:04
    关注

    Use this in place of this

    var dataString = "name = "+name;
    

    change into this

    var dataString = 'name='+ name;
    

    i have the same case here you will be guided from it.

     <html>
     <head>
      <script type="text/javascript" src="jquery-1.12.3.min.js"></script>
     </head>
     <body>
     <div class="content">
        <input type="text" class="search" id="searchid" placeholder="Search for    people" />
        <div id="result"></div>
     </div>  
     </body> 
     </html>
     <script type="text/javascript">
     $(function(){
      $(".search").keyup(function() 
      { 
        var searchid = $(this).val();
        var dataString = 'search='+ searchid;
        if(searchid!='')
        {
          $.ajax({
          type: "POST",
          url: "result.php",
          data: dataString,
          cache: false,
          success: function(html)
          {
             $("#result").html(html).show();
          }
         });
       }return false;    
       });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?