4 Replies - 1153 Views - Last Post: 16 November 2012 - 01:52 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

I'm Not Understanding How Jquery Post Works With PHP?

Posted 15 November 2012 - 03:01 PM

So I'm using this is an example but I have a show function for example and this is called when a button is clicked it looks like so

function show()
{
  $.post("quote.php", {cid: 50},
  function(data,status){
	  alert(data);
  });
}



This is called when a button is clicked and the quote.php page is

$id = $_POST['cid'];
 try
  {
  $dbh = new PDO();
  }
  catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
  exit();
  }
  $query = "SELECT Comment FROM comments WHERE ID=?;";
  $stmt = $dbh->prepare($query);
  $stmt->bindParam(1,$id,PDO::PARAM_INT);
    $stmt->execute();
	$result = $stmt->fetch(PDO::FETCH_ASSOC);
	return $result['Comment'];





That's all that's on the page and I've left the username and password for the PDO blank obviously. So when I run this query normally it works fine and there is a comment with an ID of 50 but when I use this post it does nothing like this why? I'm sure I'm doing something wrong. When the button is clicked it SHOWS A BLANK ALERT INSTEAD OF THE DATA IN THE COMMENT COLUMN

This post has been edited by adn258: 15 November 2012 - 03:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: I'm Not Understanding How Jquery Post Works With PHP?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: I'm Not Understanding How Jquery Post Works With PHP?

Posted 15 November 2012 - 03:49 PM

PHP is not something that can be called as a function from Javascript. Instead, Javascript submits a request to the server. The server then runs the PHP script and passes back the script's output. Since the only output you have in your script is in the exception handler you shouldn't see anything unless the database rejects your PDO object call.
Was This Post Helpful? 1
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3635
  • View blog
  • Posts: 5,756
  • Joined: 08-June 10

Re: I'm Not Understanding How Jquery Post Works With PHP?

Posted 15 November 2012 - 04:16 PM

In other words, use echo instead of return. The return keyword is used to return results between parts of the PHP code, not to add things to the output of the PHP code. (AJAX calls only get the output.)
Was This Post Helpful? 1
  • +
  • -

#4 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: I'm Not Understanding How Jquery Post Works With PHP?

Posted 15 November 2012 - 11:14 PM

View PostAtli, on 15 November 2012 - 04:16 PM, said:

In other words, use echo instead of return. The return keyword is used to return results between parts of the PHP code, not to add things to the output of the PHP code. (AJAX calls only get the output.)


Yes that works and this makes sense now phew! Sorry for asking such stupid questions I get this now no function like activity using returns and what not within a php script for this sort of thing i.e. it's not a function/method. Thanks for your help
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,578
  • Joined: 08-June 10

Re: I'm Not Understanding How Jquery Post Works With PHP?

Posted 16 November 2012 - 01:52 AM

some optimisation:
$query = "SELECT Comment FROM comments WHERE ID=?;";
$stmt = $dbh->prepare($query);
$stmt->bindValue(1, $_POST["cid"], PDO::PARAM_INT);
$stmt->execute();
echo $stmt->fetchColumn();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1