More correct title is to autocomplete form fields on using dropdown.
I try to use this code
<?php
// This is where you would do any database call
if(!empty($_POST)) {
// Send back a jSON array via echo
echo json_encode(array("phone"=>'123-12313',"email"=>'test@test.com','position'=>'Medicine Hat','room'=>'556 19th Street NE'));
// Exit probably not required if you
// separate out your code into two pages
exit;
}
?>
<form id="tester">
<select name="client" id="client">
<option value="">-- Select Client Name -- </option>
<option value="1">John</option>
<option value="2">Smith</option>
</select>
<input name="phone" type="text" value="">
<input name="email" type="text" value="">
<input name="position" type="text" value="">
<textarea name="room"></textarea>
</form>
jQuery(document).ready(function($) {
// On change of the dropdown do the ajax
$("#client").change(function() {
$.ajax({
// Change the link to the file you are using
url: '/test.php',
type: 'post',
// This just sends the value of the dropdown
data: { client: $(this).val() },
success: function(response) {
// Parse the jSON that is returned
// Using conditions here would probably apply
// incase nothing is returned
var Vals = JSON.parse(response);
// These are the inputs that will populate
$("input[name='phone']").val(Vals.phone);
$("input[name='email']").val(Vals.email);
$("input[name='position']").val(Vals.position);
$("textarea[name='room']").val(Vals.room);
}
});
});
});
</script>
This works perfect.
I try to use it in RSFORMS. Create form with dropdown and fields as in code above.
Php code I put in Script called on form display section.
$action = JRequest::getWord('action');
if($action == 'ajax')
{
// This is where you would do any database call
if(!empty($_POST)) {
// Send back a jSON array via echo
echo json_encode(array("phone"=>'123-12313',"email"=>'test@test.com','room'=>'240','position'=>'researcher'));
// Exit probably not required if you
// separate out your code into two pages
exit;
}
}
jquery I put in to Javascript
<script>
jQuery(document).ready(function($) {
// On change of the dropdown do the ajax
$("#list").change(function() {
$.ajax({
// Change the link to the file you are using
url: '/index.php?option=com_rsform&formId=5&action=ajax',
type: 'post',
// This just sends the value of the dropdown
data: { list: $(this).val() },
success: function(response) {
// Parse the jSON that is returned
// Using conditions here would probably apply
// incase nothing is returned
var Vals = JSON.parse(response);
// These are the inputs that will populate
$("input[name='phone']").val(Vals.phone);
$("input[name='email']").val(Vals.email);
$("input[name='position']").val(Vals.position);
$("textarea[name='room']").val(Vals.room);
}
});
});
});
</script>
My dropdown has id "list".
and in rsforms this doesn't work at all...
Any help will be appreciated....