cerberus478 Posted August 31, 2011 Share Posted August 31, 2011 Hi I have a quick question. I want my javascript to be able to access my php page because my php page has the coding to access the database and then it has to give me a list of the stuff in the database the alert box. This is the code that I have. I know that I can use it but I'm not sure how to go about it. getPHPResponse=function(){ var url="/countries/ajax_return_response"; $.post(url, {'format': 'JSON', 'countries': '$countries'}, function(data){ //this is where jsvascript must do something alert(data); }); } Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/ Share on other sites More sharing options...
flappy_warbucks Posted August 31, 2011 Share Posted August 31, 2011 That should do it. Don't forget to import jQuery before the file that script is in Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1263889 Share on other sites More sharing options...
AyKay47 Posted August 31, 2011 Share Posted August 31, 2011 insert your code into a javascript function.. then call the function when your form has been submitted.. also, your url variable needs an extension. Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1263890 Share on other sites More sharing options...
cerberus478 Posted August 31, 2011 Author Share Posted August 31, 2011 Thanks for the quick reply @flappy_warbucks when you say import jquery do you mean <script type="text/javascript" src="whatever.php" ></script> @AyKay47 I'm not sure how to do that and the countries is a controller as far as I know controllers are called like that. And I forgot to mention that I'm using zend studio. Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1263898 Share on other sites More sharing options...
KevinM1 Posted August 31, 2011 Share Posted August 31, 2011 insert your code into a javascript function.. then call the function when your form has been submitted.. Where did he say anything about a form? He's using an ajax call to retrieve data from his PHP script. It's not necessary to use a form to do it. I will say, @cerberus478, if all you're doing is retrieving info from the back end, use GET instead. That way, if, for some reason, JavaScript isn't available to your user, they can still access the data via query string. That, and it's semantically correct. GET = retrieving data. POST = updating data. also, your url variable needs an extension. No it doesn't. MVC frameworks are all about clean URLs. It's a simple matter of a .htaccess. Like he said, he's using Zend. Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1263906 Share on other sites More sharing options...
cerberus478 Posted August 31, 2011 Author Share Posted August 31, 2011 Would $.get('countries') work and how would I use .htaccess I've seen it before but never used it Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1263921 Share on other sites More sharing options...
KevinM1 Posted August 31, 2011 Share Posted August 31, 2011 Would $.get('countries') work So long as your PHP is expecting values sent via $_GET, yes. You may need to do more tweaking, depending on your needs (what URL your PHP function is at, etc.). http://api.jquery.com/jQuery.get/ and how would I use .htaccess I've seen it before but never used it If you're using Zend Framework, it may already be done for you (don't know...never used that particular framework). That said, .htaccess is outside the scope of this discussion. Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1263925 Share on other sites More sharing options...
cerberus478 Posted September 1, 2011 Author Share Posted September 1, 2011 Thank you very much nightslyr for your help. You helped me alot. Quote Link to comment https://forums.phpfreaks.com/topic/246102-quick-question-on-php-and-javascript/#findComment-1264395 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.