Hello,
As i mentionned in a previous post http://drupal.org/node/75008, i would like to use an autocomplete field and use that result to fill in another field of the form. So i found a solution in drupal 4.7 mentionned in the post. I use JS with the attribute 'Onchange'. Unfortunnatly the new FAPI make an escape an,d the quote is replace by it's hex value (').
In my module code :
$form['find']['ville'] = array(
'#type' => 'textfield',
'#title' => filt('Ville'),
'#default_value' => $session['ville'],
'#size' => 30,
'#maxlength' => 100,
'#autocomplete_path' => 'ville/autocomplete', // autocomplete Drupal
'#attributes' => array('onchange' => 'javascript:document.getElementById(\'edit-codepostal\').value=document.getElementById(\'edit-ville\').value.substr(50,5);javascript:document.getElementById(\'edit-id_city\').value=document.getElementById(\'edit-ville\').value.substr(56,8);javascript:document.getElementById(\'edit-ville\').value=document.getElementById(\'edit-vide\').value'),
'#prefix' =>'
'#suffix' =>'
'
);
But in the browser source code the result is
Ville:
What can i do to avoid this escaping ? Thank you for any help
Comments
Sorry Browser code don't appear.
onchange="javascript:document.getElementById('edit-codepostal').value=document.getElementById('edit-ville').value.substr(50,5);javascript:document.getElementById('edit-id_city').value=document.getElementById('edit-ville').value.substr(56,8);javascript:document.getElementById('edit-ville').value=document.getElementById('edit-vide').value"
Your problem doesn't make
Your problem doesn't make much sense to me... you're saying your 's in the JS are being rewritten to "? I've been using JS in 5.0 for some time now and haven't run into that problem...
On a side note, your JS needs a ; after the last variable assignment. And if you're using 5.0, you'd be a lot better off using jQuery and ditching the javascript: 's in the code there.
jQuery example: $('#edit-codepostal').val($('#edit-ville').val().substr(50, 5)); (untested)
See http://www.visualjquery.com for jQuery documentation or the ubercart link below for some tutorials in the handbooks.
----------------------
Current Drupal project: http://www.ubercart.org
----------------------
Drupal by Wombats
Thank you for your reply
Thank you for your reply, I am going to study that
Same problem
I cannot test jquery function, as the convertion of my cotes (') into ' into the browser still on.
Could anyone help me how to handle that, it is very frustrating.
- I use firefox 2.0
- Install on windows php 5 and drupal 5RC1
- use English language in drupal, but on a French config (if it is useful information)
- my FAPI source code is now;
<?php
$form = array();
$form['find'] = array('#type' => 'fieldset', '#title' => filt('Recherche magasin'), '#collapsible' => FALSE, '#collapsed' => FALSE, '#weight' => 1);
$form['find']['codepostal'] = array(
'#type' => 'textfield',
'#title' => filt('Code Postal'),
'#default_value' => $session['codepostal'],
'#size' => 11,
'#maxlength' => 5,
'#prefix' =>' <table border="0" cellspacing="0" cellpadding="0"><tr><td width="100">',
'#suffix' =>'</td>'
);
$form['find']['ville'] = array(
'#type' => 'textfield',
'#title' => filt('Ville'),
'#default_value' => $session['ville'],
'#size' => 30,
'#maxlength' => 100,
'#autocomplete_path' => 'ville/autocomplete', // autocomplete Drupal
'#attributes' => array('onchange' => "$('#edit-codepostal').val($('#edit-ville').val().substr(50,5));"),
'#prefix' =>'<td width="250">',
'#suffix' =>'</td>'
);
?>
- The HTML code in my browser is (I add bold balises in the code to avoid translation in that post)
<form action="/drupal5/?q=recherche" method="post" id="magasins-search-form">
<div><input type="hidden" name="form_token" id="edit-magasins-search-form-form-token" value="14ded0265e82ee598972f95bf5f28243" />
<input type="hidden" name="form_id" id="edit-magasins-search-form" value="magasins_search_form" />
<fieldset><legend>Recherche magasin</legend> <table border="0" cellspacing="0" cellpadding="0"><tr><td width="100"><div class="form-item">
<label for="edit-codepostal">Code Postal: </label>
<input type="text" maxlength="5" name="codepostal" id="edit-codepostal" size="11" value="" class="form-text" />
</div>
</td><td width="250"><div class="form-item">
<label for="edit-ville">Ville: </label>
<input type="text" maxlength="100" name="ville" id="edit-ville" size="30" value="" onchange="$(<b>&#</b><b>039</b>;#edit-codepostal<b>&#</b><b>039;</b>).val($(<b>&#</b><b>039;</b>#edit-ville<b>&#</b><b>039;</b>).val().substr(50,5));" class="form-text form-autocomplete" />
</div>
Try swapping your quotes to
Try swapping your quotes to double-quotes
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
.dan. is the New Zealand Drupal Developer working on Government Web Standards
Thank you Dan but
Thank you Dan for your help but the problem remain.
By changing to double quote i get this new code : "
???
Can't use double quotes in
Can't use double quotes in an onchange. ;)
I'll look into it... I hope they didn't change this for the RC1 release. hehe
----------------------
Drupal by Wombats
Ok, here's the deal... It's
Ok, here's the deal...
It's always used check_plain() to drop the code into the onchange of the form. This is what encodes your quotes. However, the browser should still interpret it correctly... that's why I never noticed it.
So, if you go to http://livetest.ubercart.org/node/43, I made a simple test with your form on a page called Nicopepe. Type 5+ characters into the Ville box and click away... see what happens to the postal code?
In FF 1.5 and IE7, that page even though it's encoded works fine.
A work around would be to put the onchange function into an included .js file or to use drupal_add_js() to put the function into the form page. Then, instead of the Javascript in your onchange, you can just put the function name and not have to worry about quotes at all! I think this is the preferred way of doing this type of thing anyways...
----------------------
Current Drupal project: http://www.ubercart.org
----------------------
Drupal by Wombats
Thank you very much
Thank you very much Ryan for your help and ok for the .js file.
This help me to focus to the real point of the problem.
Actually, the problem lies with the autocomplete function.
When i start to type few letters, i select in the list windows one item of the list. Then, it seems that the onchange event is not called when an item is selected. So i don't have the proper result . Without the autocomplete, i get the proper result as your example mentioned.
On 4.7 this was working, maybe the autocomplete function was changed ?
Ok
I used the event 'onmouseout', and i got the proper result.
Actually i add to the result string of 'edit-ville', the 'codepost' at the end ( padded with blank chars until 50 chars).
So '#attributes' => array('onmouseout' => "$('#edit-codepostal').val($('#edit-ville').val().substr(50,5));"),
put the codepost after exiting the field edit-ville.
Thank you for help, i hope this will help others.