jquery - Undefined index error in php using ajax -
<form role="form" method="post" action="test.php"> <label for="contact">mobile no:</label><br> <input type="tel" class="form-control" name="contact" title="mobile number should not contain alphabets. maxlength 10" placeholder="enter phone no" maxlength="15" required id='contact_no'> <br><br> <button type="submit" class="btn btn-success" name="submit" id="submit">submit</button> <button type="reset" class="btn btn-default" id='reset'>reset</button> </form> ajax , javascript code script type="text/javascript"> $(document).ready(function(){ $("#submit").click(function(){ var dialcode = $(".country-list .active").data().dialcode; var contact = $("#contact_no").val().replace(" ",""); var countrycode = $('.country-list .active').data().countrycode; var cn; var cc; var dc; $.ajax({ url: "test.php", type: "post", data: {'cc' : contact}, success: function(data) { alert("success"); } }); }); }); </script>
the variables show values if displayed alert message not passed on test.php page. shows undefined index error @ following statement
test.php follows
<?php if(isset($_post['submit'])){ $contact = $_post['cc']; //it shows error here } echo $contact;
i had referred many websites show same thing. dosent work me. think syntz of ajax correct , have tried possibilities still dosent work. please help
you're posting {cc: contact}
, you're checking $_post['submit']
isn't being sent. callback doesn't stop event, might want return false
(stops default , propagation). should trick:
$('#submit').on('click', function() { //do stuff $.ajax({ data: {cc: contact}, method: 'post', success: function() { //handle response here } }); return false; });
then, in php:
if (isset($_post['cc'])) { //ajax request cc data }
also not this:
$("#contact_no").val().replace(" ","");
will replace 1 space, not of them, you'll need use regex g
(for global) flag:
$("#contact_no").val().replace(/\s+/g,"");
Comments
Post a Comment