Steve Walsh
Steve Walsh

Reputation: 6645

How to pass a jquery get string to a php script

I'm sure this has been asked many times but I couldn't find the answer after quite a bit of searching.

I have a simple jquery ajax request to get some data for a particular string as follows:

$.ajax( 
   { url: "/getval.php?title="+encodeURIComponent(title), 
     dataType:"json", 
     success: function(data) { console.log(data) } 
   });

The php script is using:

$title = urldecode($_GET["title"]);

to get the value from the get request. It doesn't need to work for every conceivable string but I do need it to work for a string with a single quote. What is the safest (and easiest) way to do this request and handle the request in php?

Thanks in advance, Steve

Upvotes: 0

Views: 1809

Answers (2)

Simone
Simone

Reputation: 21272

You just need to set the data attribute, and PHP will get the value from the $_GET array:

$.ajax( 
   { url: "/getval.php", 
     dataType:"json",
     data: { title: 'the title' },
     success: function(data) { console.log(data) } 
   });

Upvotes: 1

Peter
Peter

Reputation: 16923

Why hard way?

jQuery:

$.get("/getval.php", { title: 'your title' },
   function(data){
     console.log(data);
  }
);

PHP:

<?php

$title = $_GET['title'];

Please always read manual first:

Upvotes: 3

Related Questions