Reputation: 1212
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:
<div>
with attribute and attribute value class="div1"
to get its node/inner text?<p>
child of <div class="div2">
its node/inner text?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.
Upvotes: 1
Views: 46
Reputation: 11480
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.
Upvotes: 2