Reputation: 108
Since a while I'm having trouble making the post function work. I've tried everything I found on the internet but nothing seems to help. These are my files:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script>
$(document).ready(function(){
$("#sendButton").click(function(){
event.preventDefault();
var text = document.getElementById("xd").value;
$.post("reg_send.php",
{
name: text
},
function(data){
$("#xd").html(data);
});
$("#xd").html("Trying");
});
});
</script>
</head>
<body>
<form style="width: 70%; margin: 0 auto;">
<p id="xd">TextHere</p>
<button type="button" class="mx-auto btn btn-danger" id="sendButton" data-toggle="collapse" data-target="#collapse1">Enviar</button>
</form>
</body>
</html>
And the reg_send.php file:
<?php
echo "working";
?>
But it just doesn't work, and I can't figure out why. I've tried using this too but nothing:
<script>
function send(){
$.post('reg_send',{},
function(data){
$("#Titulo").html("Xd");
});
}
</script>
And calling send() on the onClick of the button. I want to use thin o a XAMPP server, also tried setting it up there and nothing. Thanks in advance.
Upvotes: 3
Views: 898
Reputation: 122
The jquery slim does not provides support to ajax methods in order to use ajax within jquery you need to use jquery.min.js or jquery.js
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#sendButton").click(function(){
$.ajax({
type: 'POST',
url: 'reg_data.php',
data: 'Hello'
})
</script>
Upvotes: 1
Reputation: 9476
Here, You are using the slim version of jQuery, which doesn't have $.post() function of AJAX.
Use the complete build, available here http://jquery.com/download/, such as:
<script src="https://code.jquery.com/jquery-3.3.1.min.js" ></script>
Upvotes: 4