AJAX PHP Blank POST Data Sent Godaddy - javascript

I can't seem to get this simple script to work on a godaddy webserver but it works fine on my localhost. Any suggestions would be appreciated! Pretty much I've isolated that ajax is not passing the data to the other .php file, I just have no idea why. I've rewritten it many times now. It works fine locally, just not on my web server.
pleasework.php
<?php
?>
<html>
<head>
<link rel="stylesheet" href="https://cdn.metroui.org.ua/v4.3.2/css/metro-all.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
</head>
<body>
<p id="test"> please work..</p>
<form>
<button type="button" onclick="clickit()">test it</button>
</form>
</body>
<script>
function clickit() {
$.ajax({
type: "POST",
url: "worked.php",
data: { workvariable: "I hope it worked" }
})
.done(function( response ) {
alert("Success.");
$("#test").html(response);
})
.fail(function() {
alert("Sorry. Server unavailable. ");
});
}
</script>
</html>
worked.php
<?php
$workvariable = $_POST['workvariable'];
?>
<p id="pleasework">
<?php echo $workvariable; ?>
</p>

It had to do with the path. The Localhost which was WAMP didn't have any issue with it, but once on the web server it needed the full path.
Thank you everyone!

Related

Ajax query not working

I have spent some time looking at examples of Ajax, sending a variable to PHP and getting back a different variable. This is from another page (how to get variables from php using AJAX) but it seems something is not working for me. I do not get an error, just no actions appear to happen. My goal is to simply post some data to the php page and get variables back and put them in different DIVs.
htmlpage.html
<html>
<head>
<script>
$(document).ready(function() {
$("#ajaxButton").click(function() {
$.ajax({
url:'phppage.php',
data:{username: $('#username').val()},
type:'POST',
success: function(data) {
$('#result').html(data);
}
});
});
});
</script>
</head>
<body>
<input type="text" name="username" id="username"/>
<input type="button" value="Find" id="ajaxButton"/>
<div id="result">This should change on click</div>
</body>
</html>
phppage.php
<?php
$_POST['username'];
$username = $_POST['username'];
$result = '<div class="result">' .$username. "</div>";
echo $result;
?>
It is the whole scripts of yours? If it is, I think that you don't import the jquery in your frontend scripts
Please add the jquery in your code, for example :
<script src="http://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>

jQuery POST to PHP appears in browser, but not the PHP file

I've been pulling my hair out over this problem and I must have searched through over fifty other questions on here, to no avail. I'm trying to code a website using PHP, MariaDB, jQuery, and other web development technologies, however I've encountered issues trying to post variables from the client-side (with jQuery) to the server-side (PHP). I created a very simple file just to isolate the problem that I'm having:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<script type="text/javascript" src="jquery.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: "POST",
url: "index.php",
data: {'name': "name1"},
});
});
</script>
<body>
<?php
if(isset($_POST["name"])){
$i=$_POST["name"];
echo "num: ".$i;
}
else{
echo "No information supplied";
}
?>
</body>
</html>
I refresh the page, and I constantly get the failure message "No information supplied". I know the POST is getting sent to the browser, because I can see it in Firebug, and its status code is 200 OK, so I know that I must be doing something correctly. Is my PHP syntax messed up? I know that this will work just fine when I'm pulling data from forms embedded in the regular HTML.
Thank you for your assistance, and I apologize in advance if this is a really simple problem. I'm trying to learn web development as well as possible. I feel like I've tried everything, down to using the longer .ajax way of sending a POST variable, rather than using .post().
Here is your answer, you will be testing to see if there is an ajax request, if so process it, if not spit out your original page. As for the ajax call we are adding a success callback that will then update your body tags with your returned. data
<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
if(isset($_POST["name"])){
$i=$_POST["name"];
echo "num: ".$i;
}
else{
echo "No information supplied";
}
die;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<script type="text/javascript" src="jquery.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function()
{
$.ajax({
type: "POST",
url: "index.php",
data: {'name': "name1"},
success: function(data){
$('body').html(data);
}
});
});
</script>
<body>
this is the original body content
</body>
</html>
Your PHP code runs before you trigger the ajax request, because the server interprets and runs PHP before you get to see the resulting HTML in the browser
What you need is add a success callback in the ajax request object which will get the result of ajax request and do something useful with it, such as place it into a <div> element. The example below simply displays it
$.ajax({
type: "POST",
url: "index.php",
data: {'name': "name1"},
success: function(serverReponse){ alert(serverResponse) }
});

PHP file_get_contents and CURL failure to fetch the content of a javascript based page

I have tried both file_get_contents and CURL to fetch the content of a specific page. I setup the CURL to follow redirects and changed User-Agent, however, it did not work. I have no problem when loading page in browser. I get a page with below code whenever I try to fetch it with file_get_contents or CURL:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Loading ...</title> <script
src="/jquery.js" type="text/javascript"></script> </head> <html> <noscript>Enable java!</noscript> <div id="status"></div> <script type="text/javascript">
function check(){ $.ajax({
type: "POST",
url: "/index.php",
data: "allowed=5b91b80a061537ae6a23835aba38279e",
success: function(html){if(html == "allowed"){location.reload();}},
beforeSend:function(){
$("#status").html("Loading ...")
}
});
}
$(document).ready(function(){
check();
});
</script> </html>
Is there anyway to bypass such restriction without using Javascript based Web Scrapers like PhantomJs, CasperJs or ZombieJs? Simply using a plain PHP?

jQuery AJAX function returning strange output

I'm playing around with AJAX requests to a php file via jQuery, and I'm getting some strange output.
main.php:
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="/stylesheets/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<?php
include('main.html');
?>
<script type='text/javascript'>
$('#startButton').click(function() {
$.ajax({
method: "POST",
url: "test.php",
}).done(function(data) {
alert(data);
});
});
</script>
</body>
test.php:
<?php
echo 'Hello';
?>
Instead of alerting 'Hello' on button click, my program alerts the html of main.php. Any idea what's going on?
Figured it out - I'm using GoogleAppEngine and my app.yami file was not configured to direct requests to test.php, as it defaults all requests to main.php, hence the returned html.
Apologies for not stating that I was using GAE - I didn't think it was relevant.
Credit to #Dagon for suggesting that it was a redirecting error which led me to the correct answer.

$.post to php file in Amazon EC2 doesn't seem to work

i have test.html and test.php file uploaded to /var/www/html in my AWS Amazon EC2
URL of test.html is : http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.html
URL of test.php is: http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php
test.html is:
<!DOCTYPE html>
<head>
<script type="text/javascript" charset="utf-8">
function myFunction(){
var url = "http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php";
var data = "test_data_string";
alert("Post to PHP"); //check to see if javascript is working
$.post(url, {testdata:data},
function(echo){alert(echo);});
}
</script>
</head>
<body>
<button onclick="myFunction()">Click me</button>
</body>
</html>
test.php is:
<?php
$data = $_POST['testdata'];
echo "success";
?>
I am expecting to get alert("success"); when I click the "click me" button, but I am getting nothing.
i get alert("Post to PHP"); when I click on the button. so it seems that both of the files are working except $.post();
What am i doing wrong here??
These two urls are not accessible , I am not sure what you are doing but putting the php file online does not mean that it is accessible and will run as a script.
URL1: http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.html
URL2: http://ec2-11-11-111-11.ap-northeast-1.compute.amazonaws.com/test.php
Include jQuery library
update:
Your js does not have problem if you include the jquery library. I tested it sends the post request successfully. jsFiddle

Categories