Hi,

I'm new to JQuery/Ajax with PHP.
I'm calling a PHP file, and all the code runs well, except for the echo's.
The echo's are not showing and they were supposed to be showing .

Here are both my files(Which are very simple):
(x.php(main file) and sugere.php(the called file))
X.PHP

<html>

<head>

<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript" >
function sugere()
		{		


var tmp="";
$.ajax({
  type: "POST",
  url: "sugere.php",
  data: "name=John&location=Boston",
  success: function(msg){
    alert("-"+msg+"-");

  
  }

});


return false;
			}
		</script>

</head>

<body>
<form name="form1">
<?php
echo "hello";
?>
<input type="button" name="name" onclick="sugere();return false;">
</form>
</body>
</html>

sugere.php(the called file)

<?php
echo "<script LANGUAGE='javascript'>alert('uploading...');</script>";
echo "Something";
$con = mysql_connect('localhost', 'user_test', '');

mysql_select_db("test");

	$result = mysql_query("insert into user (nick) values('Jquery')" , $con);


mysql_close($con);


?>

Any ideas why the echo's are not working ?
Many thanks in advance!!

are you sure your main file is a php file?

are you sure your main file is a php file?

Hello Dschuett,

Well, i believe so. It works well, the main file 'x.php' works well in Xampp server.
I think, because i'm a newby. The problem here seems to be with JQuery, and not with PHP i think, because the file sugere.php alone works/runs well. Only when called by JQuery the echo's dont show.

Thank you for your interest and help

Member Avatar for diafol

Leave off the return false

Leave off the return false

I have removed the return false.The echo's are still not showing data.


I wonder if in both of your computers it shows the echos, does it ?

Thank you very much for your help.

Which echo is not displaying? "helo" or "something?

Member Avatar for diafol

Are all your files in the same directory? Can I suggest you just have a simple echo "me"; to begin with, just to isolate the problem to js, even though the php files runs as a standalone.

<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript" >

if (jQuery) {  
     
   function sugere(){		
      alert('function opened');
      $.ajax({
        type: "POST",
        url: "sugere.php",
        data: "name=John&location=Boston",
        success: function(msg){
           alert("success");
           //alert("-"+msg+"-");
        }
      });
      alert('function ended');
   }

} else {
    alert("jQuery not loaded");
}
</script>

Just run that for now. If problem with loaded jquery - it'll tell you. If problem running function - no alert. If no success, you'll just get the open and ended alerts. If you get 'success' - should be OK, just go back to original alert.

Which echo is not displaying? "helo" or "something?

Hi,

The Echo's that arent working are the 'something' on the called file sugere.php, the hello works fine.
It only shows the echo 'something' when the program isn's called from JQuery.When it is called alone as pure php.

Many thanks.

Are all your files in the same directory?

Hi,

Yes, all files are in the same directory

Many thanks

Member Avatar for diafol

Sorry, I edited my post while you were posting - see above...

Hi Ardav

The output is:

1-Function opened
2-Success
3-Function ended

it still doesnt show the echos
although every other code in sugere.php runs(except echo's)

Many thanks

Are all your files in the same directory? Can I suggest you just have a simple echo "me"; to begin with, just to isolate the problem to js, even though the php files runs as a standalone.

<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript" >

if (jQuery) {  
     
   function sugere(){		
      alert('function opened');
      $.ajax({
        type: "POST",
        url: "sugere.php",
        data: "name=John&location=Boston",
        success: function(msg){
           alert("success");
           //alert("-"+msg+"-");
        }
      });
      alert('function ended');
   }

} else {
    alert("jQuery not loaded");
}
</script>

Just run that for now. If problem with loaded jquery - it'll tell you. If problem running function - no alert. If no success, you'll just get the open and ended alerts. If you get 'success' - should be OK, just go back to original alert.

Member Avatar for diafol

Ah. So everything it fine except for the echo.

You did the simple file:

echo "me";

I take it? And found that the return alert was blank or that you didn't get an alert?

There's a simpler syntax:

$.ajax({
  type: "POST",
  url: "some.php",
  data: "name=John&location=Boston"
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

from the JQuery site. You could try it, but I doubt it'll make much difference.

Hi,

Exactly.
The weird thing is that in the variable msg below. The msg variable returns the echos.(Sorry i forgot to mention)
Weird isnt it ? :

$.ajax({
type: "POST",
url: "sugere.php",
data: "name=John&location=Boston",
success: function(msg){
alert("success");
alert("-"+msg+"-");
}
});

Ah. So everything it fine except for the echo.

You did the simple file:

echo "me";

I take it? And found that the return alert was blank or that you didn't get an alert?

There's a simpler syntax:

$.ajax({
  type: "POST",
  url: "some.php",
  data: "name=John&location=Boston"
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

from the JQuery site. You could try it, but I doubt it'll make much difference.

Member Avatar for diafol

The msg variable returns the echos.(Sorry i forgot to mention)

So it does work? I don't understand.

@iamthwee: Nice tutorial.

What the OP appears to be missing here, is that his PHP file is running in the background. The echo's will not display anything to the screen, but will be returned as data in the success function of the jQuery post. He really needs to read up on what callbacks are, and how to use them.

Member Avatar for diafol

Sorry to hijack ardav...

No worries - it's an open forum. All posts appreciated. :)

What the OP appears to be missing here, is that his PHP file is running in the background. The echo's will not display anything to the screen, but will be returned as data in the success function of the jQuery post.

Is that what he was referring to? If so, I misunderstood. Yes, all echoed data will be returned to the msg var in the 'success' function, with no actual change to the display.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.