Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (1 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

JavaScript
$(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;
  });
});


HTML
</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

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
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900