2

I have a ajax function that is passing a string of variables to my script but I have one variable that needs to contain a full url with parameters.

What happens is that var1 and var2 become $_POST variables but I need to save the whole url variable as a string.

var url = "http://domain.com/index.php?var1=blah&var2=blah";

var dataArray = "rooftop_id=" +rooftop_id+ "&url=" +url;

        $.ajax({
            type: "POST",
            url: "/scripts/error_check.php",
            data: dataArray,
            dataType: 'json'
        }); 

I would like my $_POST variable to look like this:

$_POST['rooftop_id'] would be '1234'
$_POST['url'] would be 'http://domain.com/index.php?var1=blah&var2=blah'

Thanks in advance!

4
  • It's still missing quotes ? Did you try it like this jsfiddle.net/6ZUzh/1 Commented May 23, 2014 at 20:01
  • url: MISSING QUOTE HERE /scripts/error_check.php", Commented May 23, 2014 at 20:05
  • Thanks! I had the quote on my testing code but passing the variables individually worked. Commented May 23, 2014 at 20:07
  • You should do this on PHP side instead, or just use GET ;) Commented May 23, 2014 at 20:10

2 Answers 2

8

Use encodeURIComponent() on url variable:

var url = "http://domain.com/index.php?var1=blah&var2=blah";

var dataArray = "rooftop_id=1&url=" +encodeURIComponent(url);

$.ajax({
    type: "POST",
    url: "/scripts/error_check.php",
    data: dataArray,
    dataType: 'json'
}); 
Sign up to request clarification or add additional context in comments.

Comments

0

Don't try to build your form data by hand. jQuery will encode it for you (with appropriate escaping) if you pass it an object.

var url = "http://domain.com/index.php?var1=blah&var2=blah";

$.ajax({
    type: "POST",
    url: "/scripts/error_check.php",
    data: { url: url, rooftop_id: rooftop_id },
    dataType: 'json'
}); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.