2

What is the best way to transfer the following outputted value into a php variable?

$(document).getUrlParam("id");
1
  • 2
    send that value to a php script using get or post request.. Commented Jul 2, 2012 at 21:37

3 Answers 3

4

Through an ajax post.

$.ajax({
 type: 'POST',
 url: url,
 data: data,
 success: success,
 dataType: dataType
});
Sign up to request clarification or add additional context in comments.

3 Comments

That was what I was originally gonna do but i didn't know if i had to post it to the same page because the php is on the same page.
@MichaelGrigsby - You are going to have to post to the server side (or get depending on security) and then you will have to reload that part of the page. How you reload the page, or part of the page, will depend on your structure.
so i can't post to the same page that the jquery is on?
3

You should use JSON format to send and receive data from javascript to PHP. For example

 function sendData(){

  //with jquery, you can use `.post()`, `.get()` or for more control `.ajax()`
  $.post(
      //URL to send the data
       url,
      //data being sent, transformed to JSON
      {dataSent: JSON.stringify(clientSideData)},
      //Do something after sendind the data to server
      function(dataReceived){
         //transform data from json format to javascript object
         var dataLog = jQuery.parseJSON(dataReceived);

         //Do stuff

      }
   )
  }

PHP:

 //Receive data from POST
 var phpVar = json_decode($_POST['dataSent'])

 //send data back to javascript
 echo json_encode(phpVar)

Comments

1

While all of these answers will get the value from javascript into the PHP script, I'd wonder if you need javascript at all.

From what I can see from a brief google the getUrlParam plugin is useful for getting and then breaking down the URL. Have you had a look at the variables available through $_SERVER? I'm sure you'd be able to find something suitable in there.

1 Comment

I write in CodeIgniter and I can't seem to get the $this->input-get() method to work. So I figured I'd try a different route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.