2

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

5
  • 3
    in your ajax data change to data:{name:name}, Commented May 5, 2016 at 5:57
  • Check the output of var_export($_POST); on the PHP side. Commented May 5, 2016 at 5:59
  • I think those spaces are causing the issue. Commented May 5, 2016 at 5:59
  • always try printing the received data from ajax before using it, so that you can be sure about what is being received on what indexes. Commented May 5, 2016 at 6:02
  • Possible duplicate of Undefined index: Error in ajax POST and/or php script? Commented May 5, 2016 at 6:03

3 Answers 3

1

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;    
   });
Sign up to request clarification or add additional context in comments.

3 Comments

And how would that help?
You can see it now @Ani Menon
Thanks its working now... just silly mistake it was... really its helpful thanks again...
1

Change to:

$.ajax({
  type:"POST",
  url:"hi.php",
  data: {name: name},
  cache:false,
  success:function(html){
    $('#msg').html(html);
  }
});

jQuery ajax data setting accept Object to customize the key. For example:

data: {anything: "123"}

In PHP:

echo $_POST["anything"]; //123

Comments

0

in your ajax data change to

 data:{ 'name' : name }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.