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.
Related
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.
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.
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 ...
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I have a form where users can click a selection of a dropdown-box.
Depending on that selection a group of names must be shown.
I believe step 1 is best to be done in AJAX but what about step 2.
After reading a lot on the internet I believe the best way is using a JSON-object. Is this the best way?
Can somebody explain me how make this work?
You can send a POST request with the input parameters via AJAX, then return an array of JSON objects from the PHP function your AJAX request calls (JSON is very easy to parse in JavaScript).
Something like (assuming using jQuery):
$.post('somepage.php',{'age':'18'},function(data,status){
if (data instanceof Array) {
// Clear the display div
$('#displayDiv').html('');
// Append all the items to the div
for (var i = 0; i < data.length; i++) {
$('#displayDiv').append('<p>'+data[i].firstName+' '+data[i].lastName+'</p>');
}
} else {
return false;
}
});
So if your list of names is on a file in your server, you have a .php file that accepts requests and you use AJAX to submit the right GET.
In your javascript using jquery, do something like
$.getJSON("names.php", {n:choice})
and in your php, do
$n=strtolower($_GET["n"]);
do whatever you need in your code, then eventually
echo json_encode($result);
and then you do whatever you want with the JSON object, which can be handled like any other js array.