When I get my result from the JS side the value comes true. But I want to fill the value to my PHP file, so I’ve used ajax. When I delete if isset function in PHP , I got an “undefined index” error.
HTML Side
<form method="post" onsubmit="return false;" class="form-inline">
<input type="email" name="email" id="subscriber_email" placeholder="Your E-mail" >
<button type="submit" id="subscribe_newsletter" class="btn newsbox-btn w-
100"onclick="gonder()">Subscribe</button>
</form>
<div id="subscribe_message"></div>
<p id="succes" class="mt-30"></p>
jS side
<script type="text/javascript">
function gonder(){
var ad=$("input[name='email']").val();
$.ajax({
type:"POST",
url:"myphpfile.php",
data:ad, // I also tried {'sendingData':ad} but it doesn't work.
success:function(sonuc){
$("#subscribe_message").html(sonuc);
$("#succes").html(sonuc);
}
})
}
</script>
And PHP side
<?php
if(isset($_POST["ad"])){
$x=$_POST["ad"];
echo $x;
}
else{
echo "There is nothing coming from the script.";
}
?>
Advertisement
Answer
You’re sending a value, but you’re not sending a key with that value. So for example if the value is 123 then you’re only sending 123, nothing else. So this won’t find it because there’s no key called ad in the data being sent:
$_POST["ad"]
Add a key for the value:
data: { ad: ad }
Or even simply:
data: { ad }