Doing something when listbox item is left clicked

General discussions related to php

Moderators: macek, egami, gesf

cincihal
New php-forum User
New php-forum User
Posts: 11
Joined: Thu May 31, 2012 7:45 am

Doing something when listbox item is left clicked

Postby cincihal » Thu Jul 11, 2013 11:07 am

I have a client listbox on a page along side other form elements related to each client. The first item in the list box is "New Client" and the rest of the items are a list of clients from a database. I would like two different things to happen depending on if a user clicks on the "New Client" or on one of the other clients in the list box. If the User clicks on the "New Client" I want to bring up a new page for filling in new client information. If the user clicks on one of the clients in the list box, I want the form elements to be populated with that particular client's data. Is this possible? Is there some event handler for a listbox that will trigger when you left click an item in the list box? I would like to avoid using buttons if at all possible.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Doing something when listbox item is left clicked

Postby johnj » Thu Jul 11, 2013 11:00 pm

Yes, this is possible. For the new client use a redirect to move to a new page. For the second case, use ajax to repopulate the form.

cincihal
New php-forum User
New php-forum User
Posts: 11
Joined: Thu May 31, 2012 7:45 am

Re: Doing something when listbox item is left clicked

Postby cincihal » Fri Jul 12, 2013 5:49 am

Are these triggered with the "onchange" event when clicking on one of the list box items or are there other ways that are typically used?

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Doing something when listbox item is left clicked

Postby johnj » Sat Jul 13, 2013 6:04 am

onchange() is possible. onclick() is also worth trying.

cincihal
New php-forum User
New php-forum User
Posts: 11
Joined: Thu May 31, 2012 7:45 am

Re: Doing something when listbox item is left clicked

Postby cincihal » Thu Jul 18, 2013 11:45 am

So I have been looking into what was said about using the 'onchange' event. Many examples, but not one I could find works for my situation. I created a form and database for testing purposes. It consists of a listbox of items (instead of clients) and four check boxes. All files including database is attached. One of the problems I'm having is changing the state of the check boxes of being checked or unchecked. The examples I have seen (using AJAX) are so different from my situation that I don't even know where to begin. If someone could give me a little help it would be much appreciated.
Attachments
Example.zip
(2.17 KiB) Downloaded 47 times

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Doing something when listbox item is left clicked

Postby johnj » Thu Jul 18, 2013 9:00 pm

Solve one thing at a time. First, try to fix what happens when a "New Client" is clicked.

cincihal
New php-forum User
New php-forum User
Posts: 11
Joined: Thu May 31, 2012 7:45 am

Re: Doing something when listbox item is left clicked

Postby cincihal » Wed Aug 21, 2013 8:24 am

So I have been working on this on and off, and have figured out in chunks some of the things I need to accomplish:

1. I have figured out how get a list box to display 'New Item' and when a user clicks on it, have a new page open for the user to create a new item, insert new data related to that new item and update the database.

2. I have figured out how to populate the form controls when the page opens with data from one of the items in the database.

What I haven't figured out is how to get the data on the form to change when the selection of the listbox changes.

The Page with form

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>


<!--Script for what is done when an item in the listbox is clicked on-->
<script type="text/javascript">
   function fillItems(id)
   {
      if (id.value === "new")
      {
         //open dialog box for entering in a new Item
         window.open ('NewItemForm.html','_self',false);
         document.getElementById('items').selectedIndex = -1;
      }
      else
      {
         //populate the form according to which item is currently selected (change the value of $id)
         //I don't know the best method for changing the $id and getting the form controls to re-populate
               
      }
   }   
</script>
   
</head>

<body>

<?php

//connect to database here
   include_once 'includes/example_db.inc.php';
   mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ('Could not connect to the database ' . mysql_error());
   mysql_select_db(DB_NAME);

//$id is the variable that represents one of the items in the list box; in this case, it represnts Item 1
//I set it to 1 just for testing.
$id = 1;

//Populate the form controls according to which id is current
$sql_query = "SELECT * FROM form_controls WHERE control_id=$id";

$result = mysql_query($sql_query);
$row = mysql_fetch_assoc($result);

$sql_query = "SELECT date_format(date,'%m/%d/%Y') as date FROM form_controls WHERE control_id=$id";

$result = mysql_query($sql_query);
$rowDate = mysql_fetch_assoc($result);

//function to close database
   function close() {
      mysql_close();
   }
   
   
// function look up and fill items listbox    
   function query() {
      $myData = mysql_query("SELECT * FROM items ORDER BY item_id");
      while($record = mysql_fetch_array($myData)) {
         echo '<option id="' . "items" . $record['item_id'] . '" value="' . $record['item_name'] . '">' . $record['item_name'] . '</option>';
      }      
   }

?>



<form action="?" method="POST">
Name: <input type="text" name="name" value="<?php echo htmlspecialchars($row['name']); ?>" />
<br />
Approximate Start Date: <input type="text" name="date" value="<?php echo htmlspecialchars($rowDate['date']); ?>" />
<br />
Checkbox 1: <input type="checkbox" name="cb1" value="0" id="cb1" <?php if($row['control_1'] == "1"){echo 'checked="checked"';}?> />
<br />
Checkbox 2: <input type="checkbox" name="cb2" value="0" id="cb2" <?php if($row['control_2'] == "1"){echo 'checked="checked"';}?> />
<br />
Checkbox 3: <input type="checkbox" name="cb3" value="0" id="cb3" <?php if($row['control_3'] == "1"){echo 'checked="checked"';}?> />
<br />
Checkbox 4: <input type="checkbox" name="cb4" value="0" id="cb4" <?php if($row['control_4'] == "1"){echo 'checked="checked"';}?> />
<br />

<!--<select name="users" onchange="fillClient(this.value)">-->
<select id="items" name="items" size="5" style="width:220px" onchange="fillItems(this);">
<option value='new'>New Item</option>
<?php  query() ?>
</select>

</form>
</body>

</html>



Some help with this would would be greatly appreciated.

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Doing something when listbox item is left clicked

Postby johnj » Wed Aug 21, 2013 7:58 pm

......What I haven't figured out is how to get the data on the form to change when the selection of the listbox changes......

When the list box selection changes, assign new values to the form field.
for e.g. if you are using jquery, to set a new value for the "age" field you can do something like this:

Code: Select all

$('#age').val('33'); or if it is the address field, it will be $('#address').val('12 Park Way, Moore St, NG 8899X3');


Return to “PHP General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest