Kane250 Posted March 2, 2009 Share Posted March 2, 2009 Hi, I'm building an application that has multiple textareas on the page, and each textarea has buttons that either insert or retrieve data to MySQL from or to the textareas. In order to not reset all the textareas on the page through standard PHP (and without going crazy with sessions), I wanted to use AJAX to make these requests. I was told that using Jquery is much better for this, but am having a very hard time finding documentation that would show me how to use Jquery with pre-existing PHP and MySQL code. I do not know Javascript, but could definitely follow instructions if a good tutorial was provided. Does anyone have any references or links? I've looked everywhere.. Thanks! Quote Link to comment Share on other sites More sharing options...
lordphate Posted March 2, 2009 Share Posted March 2, 2009 Hey Kane, If you are using a CMS, then you may have some issues with the "pre-existing" code, however not all CMS's are a PITA Basically you would need to edit the form, when you have a <textarea> box you would need to assign an ID to it, for example <textarea id="box1" name="box1"> you could then use ajax to use getelementbyid('box1') or something similar to find the box that you need to "submit without refreshing". You will need more than just an id and the 1 piece of code i've given you, i suggest looking at http://www.malsup.com/jquery/form/ they are not using the "id" in the <form> tag. I hope this helps Remember to mark it Solved if it does Quote Link to comment Share on other sites More sharing options...
Kane250 Posted March 4, 2009 Author Share Posted March 4, 2009 Oh ok, this looks simple for pulling select forms, but how would I go about getting this to make database calls through php? Quote Link to comment Share on other sites More sharing options...
Boo-urns Posted March 4, 2009 Share Posted March 4, 2009 Database calls before, in the middle of, or when submitting data? Quote Link to comment Share on other sites More sharing options...
Kane250 Posted March 8, 2009 Author Share Posted March 8, 2009 Basically what I was saying is that, I have it set up already so that I can fill textareas with ajax, but what I need to do is have ajax or jquery connect to a php script that I have written to retrieve database info and return that to the textarea. This would be done when I hit a submit button next to the textarea. Does that make sense? Quote Link to comment Share on other sites More sharing options...
Boo-urns Posted March 9, 2009 Share Posted March 9, 2009 Yea, I think that makes sense. Try something like this: Javascript: // SUBMITTING UPDATE $('#submitConfirm').click(function() { // if you need values from the form var val1 = $('#field1').val(); $.ajax({ type: "POST", url: "/includes/path-to-script", data: val1+"&nextVar="+'sending a string', cache: false, dataType: "json", success: function(data){ // all of the variables are from the json encode alert(data.response); } }); }); // END OF SUBMIT <?php // php script to return the json encoded variables. $val1 = $_POST['val1']; $str = $_POST['nextVar']; // do something $response = 'this is an fake example of your text.'; // return values $arr = array ('response'=>$response, 'error'=>$error, 'errorMsg'=>$errorMsg); // errors for displaying if there is an error echo json_encode($arr); Quote Link to comment Share on other sites More sharing options...
Kane250 Posted March 10, 2009 Author Share Posted March 10, 2009 Thanks, this was helpful. I ended up doing something similar with other code I found before I saw your post, but the basic idea is pretty much the same as you put it. Thanks again for your help Quote Link to comment 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.