Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Autoselect to populate form fields
    primarykey
    data
    text
    <p>I am trying to create a php form which uses autoselect to select an item, and from that selection, it populates the price input field and from that, populate the subtotal and total fields.</p> <p>I have a table called "accessories" which has the following fields:</p> <p>accessories_id | accessories_name | accessories_price</p> <p>I can get the first autoselect to work but Im not sure where to go from there. Im ok with php but with Javascript I am an utter novice and don't know where to start.</p> <p><strong>html file</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;Autocomplete&lt;/title&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" /&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $("#accessories_name").autocomplete({ source:'getautocomplete.php', minLength:1 }); }); $("#accessories_name").autocomplete({ source:'getautocomplete.php', minLength:1, change: function( event, ui ) {} }); $( "#accessories_name" ).on( "autocompletechange", function( event, ui ) { $("accessories_price").val(ui.item.value); //sets price with the value from selected accessory // [...] other fields logic } ); $( "#quantity" ).change(function() { var subtotal = $("#accessories_price").val() * $("#quantity").val(); //don't forget to verify if are numbers $("#subtotal").val(subtotal); }) &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form method="post" action=""&gt; Name : &lt;input type="text" size="40" id="accessories_name" name="accessories_name" /&gt; Price: &lt;input type="text" size="30" id="accessories_price" name="accessories_price" /&gt; Quantity: &lt;input type="text" size="30" id="quantity" name="quantity" /&gt; Subtotal: &lt;input type="text" size="30" id="subtotal" name="subtotal" /&gt; &lt;br /&gt; Name : &lt;input type="text" size="40" id="accessories_name" name="accessories_name" /&gt; Price: &lt;input type="text" size="30" id="accessories_price" name="accessories_price" /&gt; Quantity: &lt;input type="text" size="30" id="quantity" name="quantity" /&gt; Subtotal: &lt;input type="text" size="30" id="subtotal" name="subtotal" /&gt; &lt;br /&gt; Total: &lt;input type="text" size="40" id="accessories_total" name="accessories_total" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>php file</strong></p> <pre><code>&lt;?php mysql_connect("localhost", "db_user", "password"); mysql_select_db("db"); $term=$_GET["term"]; $query=mysql_query("SELECT * FROM accessories WHERE accessories_description LIKE '%".$term."%' ORDER by accessories_description"); $json=array(); while($accessories=mysql_fetch_array($query)){ $json[]=array( 'value'=&gt;$accessories["accessories_name"], 'label'=&gt;$accessories["accessories_name"]." - ".$accessories["accessories_id"] ); } echo json_encode($json); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload