MockY Posted September 6, 2013 Share Posted September 6, 2013 (edited) I do an Ajax call when the user tabs out of a text box using onblur(). It works just fine but I need to pass along variable values that have yet been set. To show what I mean: <input type="text" name="comment[]" onblur="loadurl('ajax.php?update_record=<?php echo $r['Id']; ?>&comment=')" /> The loadurl() function fires the Ajax call and will do it's thing. Problem is, I want the comment variable to have the value of whatever the user types in the box before the user leaves the box. All the script now has to work with is the Id of that specific record. My thought was to add a javascript variable that will get the same value as what the user types using onkeyup or similar but don't know how to do it Any help is greatly appreciated. Edited September 6, 2013 by MockY Quote Link to comment https://forums.phpfreaks.com/topic/281931-set-variable-from-textbox/ Share on other sites More sharing options...
Solution kicken Posted September 6, 2013 Solution Share Posted September 6, 2013 Just concatenate this.value onto the URL. Use encodeURIComponent to take care of any special characters. Quote Link to comment https://forums.phpfreaks.com/topic/281931-set-variable-from-textbox/#findComment-1448504 Share on other sites More sharing options...
MockY Posted September 6, 2013 Author Share Posted September 6, 2013 Thanks you so much. Works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/281931-set-variable-from-textbox/#findComment-1448516 Share on other sites More sharing options...
MockY Posted September 6, 2013 Author Share Posted September 6, 2013 (edited) However, this got me thinking about making it a little trickier. I should probably start a new thread, but it so closly related to this question What if I had 2 text fields, and I wanted to fetch the value from the second text box as well. Since I can't use this.value as I'm not fetching value for the active box, what would I use instead. I tried the following, but that did not work. onblur="loadurl('ajax.php?update_record=<?php echo $r['Id']; ?>&net=' + encodeURIComponent(this.value)) + '&comment=' + document.getElementById('comment').value" EDIT: Removing document. made it work Edited September 6, 2013 by MockY Quote Link to comment https://forums.phpfreaks.com/topic/281931-set-variable-from-textbox/#findComment-1448522 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.