Using PHP, Javascript, and HTML together [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using jQuery and Ajax with other scripts, getting data (sentences) from a MySQL database, calling PHP scripts with json_encode. However, I've found that json_encode doesn't handle certain characters (like a British pound sign, £). I need to clean up these sentences. So, I have a PHP script that gets the sentence ($sent) in its full form. I want to put it into an HTML div element. But, how do I pass the value of this variable to the jQuery ready function?
`
$(document).ready(function () {
//$('div.inst').text("");
$('h2').text("");
$('h2').text("");
});
`
In the PHP, in response to a MySQL query
`$sent = $row->sentence;`
I put the Javascript before the PHP. Does that matter?

You can use something like
utf8_decode(json_encode($data_to_be_returned))
Btw, if you're fetching your data from some mysql query, you can use something like mysql_query('SET CHARACTER SET utf8')

you can just echo php out into your javascript. PHP executes on the server; JS on the client side.
jQuery(document).ready(function(){
doSomething("<?= $sent ?>")
});

Tried with: json_encode($whatever, JSON_UNESCAPED_UNICODE); ?
PHP 5.4 is needed for that...
Sounds weird that php is complaining about a dollar sign, what encoding is the string you are getting ?
About passing the json to the javascript i think you should do a php page who can get called by the ajax request and return that json_encode output as plain text with the appropriate headers, accepting the arguments you need ...

Related

How to refresh only a specific function in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a function to show current date and time using date() function in php.
I used
meta http-equiv="refresh" content="30" to refresh the whole page.
But i want to refresh only that specific function part as i want to change the time every minute. Is it possible?
In short, no it is not possible with just PHP and HTML.
Once your page has been generated by your PHP script, it is sent to the client and cannot be modified anymore. HTML is made to build "static" web pages, that mean they won't produce fancy moving things and therefore they won't update content once the page is loaded.
But even if you cannot send the page a second time once it has been generated by PHP, you're not bound to HTML in the page. You can set up script in the page that will for example make the client browser perform a request to your server to update part of the page. That's called AJAX, and to do it you have to learn Javascript.
You should use client-side magic for this, so I'd use AJAX techniques.
setInterval(function(){
$("#time").load("page.php #time");
}, 30000);
Justin E asks, you can actually load a specific element from a page. Yes you can.

Get window size with php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the problem this code:
$window_width= '<script>screen.availWidth</script>';
$window_height= '<script>screen.availHeight</script>';
any idea?
Nothing wrong in your code, but you can't get any values in PHP variable, because PHP is Server side scripting language.
You need JavaScript, not PHP.
var screenWidth = window.screen.width,
var screenHeight = window.screen.height;
You can then send it to the server via Ajax (with an XmlHttpRequest).
You seem to be misunderstanding the basic concepts of how PHP and HTML/JavaScript works together. PHP is a server-side language and JavaScript is a client-side language.
PHP generates the output first and then it's transferred to the client where it's being executed. You're trying to do it the other way around.
What you need to do is first, generate a page using PHP, have it sent to the client for client-side execution, and from there have JavaScript perform a call that sends the information back to PHP using a GET/POST request.

Echo Execute PHP Code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to execute PHP code in echo? I need this to finish my work, but everything I try seems to be fruitless; I often bump to either blank browser page or some errors.
It does not need to be with echo func. Here is example of the code I would like to compile. Just a sample nothing flashy. Echo is just example, I would like to put more complex and advanced code at echo's place but I would like to start from simple stuff like that.
<?php $code = "<?php echo '123'; ?>" echo $code; ?>
That is....an epic fail catastrophe. But for whatever reason, I'm answering... You would need to use eval(). But don't do it. Read here: http://php.net/manual/en/function.eval.php
Perhaps you just need to use include 'path/to/file/with/my/code';. Read here: http://php.net/manual/en/function.include.php
Based on your comments, I think you're actually looking for this:
echo htmlspecialchars(file_get_contents(__FILE__));
That will display the php code that ran. You could search for that line and remove it before displaying. You could even write a function to look at all of the included files and add them to a $variable to be displayed, using the same logic.
Php is only executed on the serverside, so when you try your echo code, you are saying print this code. If you want your code to validate/run logic after it has been server from the server, i would recommend looking into javascript and AJAX requests.
Good luck :)

Passing data from HTML page to PHP script [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I currently have a PHP script that is being called from an HTML link. The script is set up such that it needs to accept non-user input from the page (static data that already exists). The line that calls the script looks like this.
<div class="Name">Text</div>
I need to pass the values in "Name" and Text to the script, as well as another value from an earlier line.
What would be the best way to accomplish this? All of my research points to using forms and GET/POST, but as you can see, there is no place for the user to input any of the data. Is there any way to do this using hidden forms or AJAX?
If you're the author of the web page, you'd use javascript and an onclick event to capture the div class and the anchor's text, and send it via ajax (or directly if your script provides some sort of user output or redirect back to the calling page) to your script as a post event. the data could be conveniently formatted as a json structure to simplify the script's processing.
By using the GET method :
<div class="Name">Text</div>
Maybe this code well help
<div class="Name"><a id="link" href="some/script.php">Text</a></div>
<input id="name" onblur="modify(this.value)">
<script type="text/javascript">
var link = document.getElementById("link").href;
var modify = function(name){
var a = document.getElementById("link");
a.href = link + "?name="+name;
}
</script>
In the href attribute for the link, do something like this:
href="some/script?name=George&text=All This Stuff"
This will make the information available in the $_GET[] array in the PHP script.

Accessing javascript object from PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am storing some functions in an object, and these functions need to be accessed in PHP via the onclick attribute of the dynamic button. Normally, this would be very simple if the functions were not stored in an object, but unfortunately, this is how these functions need to be created:
var arrayLength = <?php echo $arrayLength; ?>;
var click = {};
for (var num=1;num<=arrayLength;num++) {
var newClick = "click_" + num;
click[newClick] = function() {
// some contents when this button is clicked
};
}
To call them in javascript, I'd do something like:
click['click_' + someID]();
However, since I'm working with PHP, I am not so sure how I will be able to call this in the onclick attribute. It is something I really need to do, so if you know a solution or can think of a workaround, let me know.
First: What is so special about your function that you have to write it's body in JS and call it in PHP?
Second: Why not just write that function in PHP and pass data from JS via AJAX?
Third: make double sure you know difference between http, html, php, javascript and AJAX and understand what these are, not just some wibly-wobly stuff that make web pages work. Then you will know your answer yourself.
Your objects can go from PHP to JS, but not the other way around. In order to alleviate this issue, you can use json_encode($object) and pass it to the javascript to be processed. From there, you can then access the properties and values from PHP.

Categories