Get text of curtain element ID in PHP [duplicate] - javascript

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I need to parse the text out of an h3 element on an HTML page and save the text into a variable.
<h3 class="names-header">Names</h3>
I need the output:
Names
Saved into a variable like
$text = $output;
I've tried using DOMs, specifically this example but I've had no luck.
I've also tried to extract the data using JQuery, and submitting it as a post using Ajax on the same page. Then grabbing the post and saving it in PHP. This also didn't work, and it seems like there is a much quicker way to do this.
I've googled and tried for around 2 hours now and still can't figure out how to fix it. Any help/advice would be greatly appreciated right now. Thank you.

it would be easy to use jquery to do this! just use ajax like in the following code.
$.ajax({
type: 'POST',
url:'your php page',
data:{name: $('.names-header').text()},
success:function(response){
alert(response);
}
})
in you php do the following.
if(isset($_POST['name'])){
echo $_POST['name'];
}else{
echo 'no data to show';
}
this will allow you to catch the post data and do what ever you want.

Related

Retrieving Javascript variable into Php code from empty div tag [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
1. <script>
2. document.getElementById('description').innerHTML = description;
3. </script>
4. <div id="description"></div>
How to get the value of line 4 into PHP code?. Also when I use the traditional way
<?php $weather = <div id = "description"></div>?>
The weather variable is not displayed wherever I want it to be displayed.
Any help is greatly appreciated. Thank You.
The PHP is processed on the server side, while the Javascript value is processed in the user's browser. So when the page is sent, there is no value there yet.
You can do this:
<?php
$weather = '<div id="description">Details go here</div>'; // define variable
echo $weather; // include / print variable
?>
But if the value does not exist yet that you want to include, then you need to use AJAX to send it to PHP if that's indeed what you need. Although if it doesn't need to go back to the server, it can likely be done just with JavaScript on the client side.
I suggest finding some good books on learning PHP / learning JavasScript / learning HTML.

Need help breaking down JSON string in PHP [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 7 years ago.
I need to be able to grab some data from a string using PHP.
I got an API from the games website and need to break it down.
The string I need to break down is this:
http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513
I need to get the small icon image from that string which is the first url in the string, and the current price, which in the string is 887.
So, where it states this:
"current":{"trend":"neutral","price":887}
I need to grab the 887 and put it into a variable.
I'm using PHP,
thanks in advance if anyone can help :)
Use json_decode() for this purpose:
$item = json_decode($json)->item;
$price = $item->current->price;
$icon = $item->icon;
Download the JSON and convert to an object.
$item = json_decode(file_get_contents('http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513'))->item;
$name = $item->name;
// etc.
Here's a function you should keep handy. It is useful for printing formatted objects and arrays, which makes it much easier to examine their structures.
function debug($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}

Pass a variable from a javascript function to another php file [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.
Thanks
There are several ways you can do this. Probably the simplest way is:
var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;
Then your script 'otherscript.php' can access that variable with $_GET['myData'].
This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.
Something like that should work:
<script type="text/javascript">
function yourFunction(yourParameter) {
<?php $abc = "<script>document.write(yourParameter)</script>"?>
}
</script>
<?php
echo $abc;
?>

Injecting PHP data into JavaScript code [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
What's the preferred way of outputting PHP code within apostrophes? For example this JavaScript code would break and the second line will be totally empty:
var jsString = '<div id="content">'+'<?php echo $something['1'] ?>'+'</div>';
Thanks! I know this is kind of basic and I know how to fix it, but I'm wondering how to do it the right way?
[edit]
I know it looks obvious, but $something["1"] won't work. The same goes for $something[\'1\'].
This example is working well for me :
<?php
$something['1']='hhhh0';
?>
<script type='text/javascript'>
var jsString = '<div id="content">'+'<?php echo $something['1'] ;?>'+'</div>';
alert(jsString);
</script>

Quotes are escaped when passing JSON as a POST variable [duplicate]

This question already has answers here:
Why are $_POST variables getting escaped in PHP?
(6 answers)
Closed 9 years ago.
I'm doing an Ajax call to my server and need to send an array. I'm encoding the array using JSON. That results in this data sent to the server using a POST request:
selection=%5B%221%22%5D
On the server, I have this code:
echo urldecode($_REQUEST['selection']);
This results in:
[\"1\"]
Note that there are no backslashes in the request. I checked that with Firefox's dev tools.
Where were the backslashes added? Am I doing something wrong here? I can't decode the string like this.
This is the client-side code:
$.ajax({
type: "POST",
url: "<my-uri>/rule/add.php",
data: {
selection: JSON.stringify(["1"]) // in reality this is a variable array
}
}).done(function(data){
alert(data);
});
This happens because your server is configured to add slashes to quotes.
If you wish to avoid this go to your php.ini and set magic_quotes_gpc to 0.

Categories

Resources