0

Goal: I want to get the text from the selected element and then create a PHP variable from it.

I know how to do this using jQuery but can't wrap my mind around the idea of how to do it in PHP. Basically lets say I have a table with some data. When the user clicks on the data I would like to retrieve that text and then place it in a PHP variable. I could use .text(); from jQuery to create the variable but I could not pass that variable into PHP for further use.

I am very new to PHP (a front-end developer trying to learn back-end). So any explanation would be helpful also.

thanks!

***OK I now understand how to do it. Thanks for the input. I was not thinking. It makes perfect sense to create the variable using a _post and then returning the data via AJAX. Sorry for wasting your time. Thanks for replying.

2
  • This is not possible in a direct way, because the data flow is from server to browser, never the other way around. So you either need AJAX or some form of POST request to get this done.
    – Luwe
    Aug 24, 2011 at 20:15
  • PHP is a server-side language so it doesn't interact directly with client-side operations. Sounds like you need AJAX.
    – Adam Moss
    Aug 24, 2011 at 20:16

7 Answers 7

2

There is no onclick event in the backend, at least not that works like in javascript

if you want to pass a variable to the server you can use a form and send the data through a GET or POST

Example:

<form action="index.php" method="get">
 <input type="button" value="somevalue" name="action" />
</form>

when the user clicks you will see this in the url

www.example.com/index.php?action=somevalue

now you can retrieve this value in php using the $_GET global variable

$variable = $_GET['action'];

The same can be done using POST, this way the variable is not shown in the url

1

In order to have PHP gather any client side data you would have to pass that data back to the server.

To assign data from client action to a PHP variable, you would first have to capture that data using a front end solution (like your jQuery one above) then pass that information to your server application for processing using AJAX or form submission.

Your PHP data handler could then parse that information out from the data submission and push that info into a variable.

Your front end would look something like

$.ajax({
    type: "POST",
    url: 'your_file.php',
    data: 'selected='+ captured_data,

});

and on your back end...

<?php
 ...some php code...

 $var_to_hold_selected_thing = $_POST["selected"];

 ...more php code
?>

Specifics of how to do more than that would depend on your application...

1

You can do it with ajax:

$.ajax({
    type: "POST",
    url: 'your_file.php',
    data: 'text='+ text,
    success: function(data) {
        alert(data);
    }
});

After the .click() event you could have a dialog box appear, with Is this your text?. Then post it.

1

You could grab that text with jQuery using $(elem).text() as you've described and then pass the captured value to PHP using an AJAX request with jQuery (see $.ajax). The PHP script you pinged with AJAX (either via GET or POST) can then take the value you passed and push it into a $_SESSION variable.

session_start();
$_SESSION["text_clicked"] = $_GET["text_string"];
exit;

After you do this, any subsequent ajax calls made with jQuery (to any PHP script), can recall this variable, provide that you've properly initiated the session.

1

Stack 101's answer is the correct execution that you're looking for, however it sounds like you could use a little more background on the context first.

Basically, PHP is a server-side scripting language, which means that it gets executed before the page loads into your browser. Here it can look at a database, connect to other sites, or do a whole host of other stuff. Once it's in the browser, your HTML + CSS + jQuery is in charge of displaying + styling + and manipulating that information respectively.

AJAX is usually used to describe the act of posting data to a PHP script, and then re-loading it back into your page without the page refreshing. In other words, jQuery passes data to a PHP script that runs on the server, and then loads in whatever the PHP script outputted. Sometimes PHP can output an entire site (html tags, css, js, whatever). Other times it can simply access a few variables that it got from the POST array, and output a result. This is much more common with AJAX requests.

A quick search turned up this tutorial which might help (I haven't followed it)

http://www.php4every1.com/tutorials/jquery-ajax-tutorial/

Good luck!

0

I believe it could be done by sending all the required content through GET or POST, and then make PHP stuff it to a variable, however I'm not exactly sure.

0

PHP runs on the server. Variables come and go each request. Everytime you refresh a page (or send a request otherwise), a PHP script starts running, spits out a response, and stops again. So setting just a loose variable is not really an option.

The options you got are - Use a cookie. You can set it from Javascript and it will automatically be sent with the next request. Your PHP script can read this cookie. - Send the variable by doing an AJAX (asynchronous) request. You can send the value in the url or as posted data.

In either case, you can decide to use the variable only once, or store it in a session. I think the cookie will remain unless you explicitly clear it. So you could keep using that.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.