1

I want to JQuery while defining a variable in PHP.

Suppose I have the following index.php file:

<!DOCTYPE html>
<html>
<head>
    <title>Page</title>
    <script type="text/javascript" src="../../../resources/js/common/jquery-latest.min.js"></script>
</head>
<body>
    <div class="bigdaddy">
        <div class="daddy">
            <header class="main"></header>
            <main>
                <aside>
                </aside>
                <article>
                    <div class="div1">
                        Sample1
                    </div>
                    <div class="div2">
                        <div> //Child 1
                            Sample2
                        </div>
                        <p> //Child 2
                            Sample3
                        </p>
                    </div>
                    <?php
                        // the code
                    ?>
                </article>
            </main>
            <footer class="main"></footer>
        </div>
    </div>
</body>
</html>

Questions:

  • How can I select <div> with attribute and attribute value class="div1" to get its node/inner text?
  • How can I select <p> child of <div class="div2"> its node/inner text?
  • Is it even possible to use jQuery in PHP?

Both questions are using jQuery

I thought of

<?php
    $answer1 = $('div[class="div1"]').html();
    echo $answer1;
    $answer2 = $('div.class="div2"').children('p').html();
    echo $answer2;
?>

But it didn't work.

Expected result should be:

Sample1 Sample3

EDIT: I can't use AJAX.

17
  • Is it even possible? Should've been first question. Commented Jan 4, 2017 at 7:46
  • Your code is not working because you are trying to use jQuery in PHP. If you need to pass values to PHP you should to get them via jQuery and use ajax to pass them to PHP script. Commented Jan 4, 2017 at 7:47
  • No its not possible. Or at least not how you try. You can't use Javascript inside of PHP. Javascript is running clientside, PHP is serverside. They can't communicate the way you try. You have to pass the values from JS to PHP with for example AJAX. Edit: Blady was faster :D Commented Jan 4, 2017 at 7:49
  • 1
    You need to understand the client and server. all the javascript/jquery code runs in browser which happens very late as the response from the server already ran and displayed at browser. Commented Jan 4, 2017 at 7:50
  • 1
    Take a read at this: sqa.org.uk/e-learning/ClientSide01CD/page_18.htm Commented Jan 4, 2017 at 8:19

1 Answer 1

2

You can use AJAX to send the data you want to the server, and handle the rest using PHP:

var answer1 = $('.div1').text();
var answer2 = $('.div2').find('p').text();
$.ajax({
  type: 'post',
  url: 'your_php_script.php',
  data: {
    answer1 : answer1, answer2: answer2
  },
  success: function(response_from_php_script) {
    console.log(response_from_php_script);
  }
});

And in your PHP script you can get the value of the paragraph you need like this:

 $answer1 = $_POST['answer1'];
 echo $answer1;
 $answer2 = $_POST['answer2'];
 echo $answer2;

Of course you will have to sanitize the post values if you want to insert it in the database, but that's a whole new thing.

I hope this will get you started.

You can read more about AJAX here.

Sign up to request clarification or add additional context in comments.

1 Comment

@A.Lau, saw that, but as long as he uses jQuery I don't see why can't he use AJAX.

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.