Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Is it a common thing to mix PHP and JavaScript?
Lets say I declared a PHP function returning a string:
function myPhpFunction(some_string) {
return 'blablabla' . some_string;
}
Then once the DOM is loaded is it correct to do something like:
$(document).on('click', '.my_class', function() {
$(this).html(<?PHP myPhpFunction('some_string'); ?>);
});
Not exactly like that, but there are ways to achive what you want to do!
But first, some basics:
PHP is a server side langauge. It processes data, makes HTML, send the HTML and javascript back to the browser.
Javascript is a client side language. It works in the browser and not on the server side (apart from node, but we won't go into that).
What you want to do, is pass some information from your PHP to your javascript.
You would put the javascript code you have there, in a PHP file. The PHP code will then replace that section with the resulting string, which will result in that value being sent to the client in the HTML.
If your javascript function or event or whatever, is ran multiple times, it won't be able to run the PHP function again, as it only runs once to generate the text of the HTML which is sent back to the client.
Hopefully that all makes sense.
P.S. remember to search stackoverflow for your question before asking it. I'm pretty sure someone has asked and answered this question before. Normally it will be flagged, but I'm feeling nice today, so you get an answer.
Feel free to ask further questions if I don't make complete sense.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Sorry in advance if what I am going to ask is silly, I am having trouble understanding the asynchronous HTTP responses. I am trying to use jQuery .ajax() with PHP and MySQL in the following context:
when page loads I will query and return all the applications for a particular id, and then I will use a while loop to output them;
also there is a button that allows the user to add new application for that particular id -> on click it:
shows a hidden form and gets the values in the inputs;
sends the data to PHP using the .ajax() method;
performs the insert.
and I am stuck at this point
I need to output the newly added application above the existing ones, but the query and while will do it only when an synchronous HTTP request is sent.
I tried to use the .success() but the HTML I need to output inside is really long and I am afraid it will be hard to maintain having outputs from both PHP while loop and jQuery.
Can you please help me understand an efficient way to do this? I have never dealt with asynchronous HTTP request before. Also, I am can't use any JavaScript templating libraries.
I would really appreciate your help!
An ajax postback typically implies some sort of JSON-based web api behind it. Examples of this on the internet abound.
https://www.lennu.net/jquery-ajax-example-with-json-response/
http://www.9lessons.info/2012/05/create-restful-services-api-in-php.html
Your REST endpoint should return the minimal amount of JSON data you need. At which point you can use that data to update/bind to elements in your DOM inside your "done" or "success" callback.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I got input type field and value in that field is changing when I move bar on range slider.
Can someone help me to write some if condition.
I want when the value is 1 to echo "one"... when value is 2 to echo "two"...
here is range slider and input type="text" with values.
link to page I am trying to do :)
Thanks!
bored enough to bother:
$out=array('1'=>'one','2'=>'two'); //etc
echo $out[$_POST['selected']];
Your question, as phrased, is going to be difficult to answer. Here's why:
You are working with an interaction between an HTML web form and a PHP script. However, you didn't provide any example code, so it's impossible to know how you are setting up this interaction.
It might be better to approach this purely as a javascript or jQuery solution - but without knowing what you are doing with the end result, it's impossible to make that judgment or offer guidance.
If you are hoping for a strict PHP solution, how would you like the data returned - if you need it returned at all. A PHP script to do what you ask is fairly simple - but the return portion can be involved.
Perhaps you're simply looking for the PHP switch statement?
Are you actually trying to convert an int or float to it's string text? Take a look at this answer...
Finally, you might want to review the write-up on asking questions. You'll get better answers.
mnr
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have inherited a php site that is returning HTML pages. I always thought that the server returns data to the client and the client decides how to show the results.
Even though this is working, is this not a very tight coupling between the server and the client?
I would have thought a much better way to handle this is for the client code, javascript or gwt or what have you to ask for the needed data and the server returning that data only such as JSON object or a similar thing.
Thoughts on this?
It sounds like you could benefit from making an AJAX call (via js) to a php page, then manipulating the data (JSON object, string of comma delimited data, raw HTML, etc.) returned on the client side.
Sorry for the bad previous example, this example is a more sophisticated, modern example of how an ajax call should be made.
It appears that it is not at all uncommon for php scripts to return HTML. It does create tight coupling with the client application. Returning JSON does create a more loosely coupling with the client.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
what way should I structure my files when doing Jquery AJAX requests in PHP.
i.e.
showRecords.htm - contains jQuery/AJAX that gets data (HTML) from the php file below.
showRecords.php - contains php that does some processing, likely to get data from DB and echo some HTML.
or
one file:
showRecords.php - contains the JQuery/AJAX and is the php file and requests data from itself.
also you clever people out there, please note I am a beginner in JQuery
and AJAX so please forgive me if its a silly question or trivial.
Obviously not: one file.
I'd do it this way:
showRecords.php - form processing, db requests
templates/showSomeHtml.php - displays everything that your users can see
showRecords.js - jQuery/AJAX that gets data
And if the jQuery/Ajax modifies the HTML, you could also move that (the dynamic HTML) into the templates (read about javascript templates)
I think it's up to you. If you have a quick ajax call you'd like to do and you know it won't be used anywhere but there then I see no issue with having everything in one file. On the other hand, you might need to access that processing script in multiple locations on your site. If that's the case, it's not ideal to place it all in one file. I think two files avoids confusion. Keeps everything separate and neat.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Personally I would do this with PHP but maybe someone has an pro/contra for doing this with JavaScript:
I (will) fetch a few song details (about ten per page load) from Soundcloud
afterwards the statistic (played this week/total, liked this week/total etc.) will be displayed in a table
That's it - nothing special.
I can solve it like this:
let PHP do the stuff and render the whole page at once
This will of course take some time before the page is rendered.
Is there any advantage of doing this with JavaScript? I can only think of:
the page renders faster (with the disadvantage that the results may not be displayed instantly)
I'd say that doing it via JavaScript will actually take longer, as you'll have to connect again. Although, it may give an illusion of "being faster" (though I doubt it'll be noticeable).
I suppose it might depend on your usage cases too.
How are you handling authentication with SoundCloud?
I would honestly go the jQuery route.
Use a $.post() call, and within your success statement, loop through the json and append it to a table. This way, you can do something like pagination, which can update your table without needing to refresh the page.
Do it serverside and pre-load the page. The soundcloud API should not be the bottleneck here so reducing a request or two is worth it.