Jump to content

quick question on php and javascript


cerberus478

Recommended Posts

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);

});

}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.