views:

56

answers:

2

In PHP, in a particular CMS I am using a custom field, which works like google suggest.

As in, for each letter I type an SQL query is performed and matching records are displayed. When clicking on a record it fills the field with that record.

I am fairly certain this is all done with JavaScript.

I need to know how I can access the resultant content of that field, with the text placed through JS, before it is submitted so I can explode() it.

The CMS I am using is using mootools, so a solution relying on mootools would be ideal.

A: 

You could try to use JS to send the field on some event (onkeyup?) to your php script. After it does it's part, store the result as a session variable and you can retrieve that later. Try using jquery's get function.

Was that your question?

misterte
A: 

(This answer assumes that you have control over the markup of your forms (the form that requires a string "explosion" before submit) and/or you feel comfortable tinkering with whatever plugins you're using.)

first, make sure that you aren't submitting your form using an actual submit button (). We'll need to submit the form using javascript after fiddling with the field's contents.

next, make sure that your input box (the one you're grabbing text from) and your hidden inputs have unique ids. This will make it easier to query the DOM for the data we need.

Inside your form, in place of a "real" submit button, create a form button:

<form action="something.php" name="myform">

   <input type="hidden" id="hiddenItem">
   // SOME STUFF

   <input type="text" id="autocomplete_field" value="whatever"/>         

   // SOME OTHER STUFF

   <input type="button" value="Submit" onclick="processForm(this)"/>

</form>

Then, write a javascript function to process the string and submit the form:

processForm = function(el){

   text = $('autocomplete_field').get('value');

   // Lets assume the strings separates words (what you're exploding apart) using spaces
   // something like 'DOGS CATS BIRDS PETS'

   var array = text.split(' ');

   // returns ['DOGS','CATS','BIRDS','PETS']

   $('hiddenItem').set('value',array[0]);

   // #hiddenItem now has the value 'dogs'

   //SUBMIT THE FORM
   el.getParent('form').submit();

};

Hope this helps!

objectUndefined