Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
i am trying to pass a JavaScript variable to php using ajax, by calling the same page
and passing the variable to a bootstrap modal, for some reason I am getting the following error:Notice: Undefined index: name

$(document).ready(function(){
  $(document).on('click','.idname', function(){
      alert('est123');
    //e.preventDefault();
    //var data = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'index.php',
      data: ({name:"test"}),
      cache: false,
      success: function(data){
        $('#results').html(data);
      }
    })
    return false;
  });
});


</head>
  <body>
 
	
  <!-- Trigger the modal with a button -->
<button type="button"id='idname' class="btn btn-info btn-lg idname" data-toggle="modal" data-target="#myModal">Open Modal</button>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <p><?php echo  $_POST['name']; ?></p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</div>
  </body>
</html>
Posted
Comments
Amit Jadli 25-Nov-15 2:15am
   
Post your index.php (php code).

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

try
$(document).ready(function(){
  $(document).on('click','.idname', function(){
      alert('est123');
    //e.preventDefault();
    //var data = $(this).serialize();
    $.ajax({
      type: 'POST',
      url: 'index.php',
      data: {name:"test"},
      cache: false,
      success: function(data){
        $('#results').html(data);
      }
    })
    return false;
  });
});

That is remove the "()" around the data
   
v2
Comments
Richard Deeming 26-Nov-15 11:09am
   
Nice, but you should wrap your code block in <pre>...</pre> tags instead of <code>...</code> tags.

See how much nicer it looks now? :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100