Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-23-2009, 02:42 PM Dynamic Form
Average Talker

Posts: 16
Trades: 0
Hi all.

I have a basic form with a drop down list then some text boxes whose purpose is to edit data stored in a database. The user would select what they want to edit from the drop down list and the current values would dynamically appear in the text boxes ready to be changed as required.

Aside from the database, the form and populating the drop down I have nothing! I am aware that I could use a separate php script through the form action but I don't want to add an extra click as the user has already made two to get to this page.

I therefore am thinking down the route of a java onChange event, which in know isn't php but both languages are going to need to be incorporated, aren't they?

Any suggestions?

Thanks,
Dan
superdan_35 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-23-2009, 06:00 PM Re: Dynamic Form
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
May I suggest AJAX together with php? With a framework such as for example jQuery AJAX is really simple, and when selecting in the drop down you could get data from a php file, which would select the needed stuff from the database etc. and return it, then some simple JavaScript (with jQuery) to add it to the page.

I've recently done something kinda' similar and it wasn't too hard, evan though I'm no good at JavaScript at all. Thumbs up for jQuery :P
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-24-2009, 01:20 AM Re: Dynamic Form
dark_lord's Avatar
Experienced Talker

Posts: 41
Name: Parijat Roy
Location: INDIA-KOLKATA
Trades: 0
@=superdan_35

Quote:
I therefore am thinking down the route of a java onChange event, which in know isn't php but both languages are going to need to be incorporated, aren't they?

you can not execute php queries client side, which means you cannot use simple JavaScript to execute php page, which means you need to use AJAX as said by lizciz to attain what you looking for.
__________________
I AM THE BEAUTIFUL NIGHTMARE

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
dark_lord is offline
Reply With Quote
View Public Profile Visit dark_lord's homepage!
 
Old 03-24-2009, 05:57 AM Re: Dynamic Form
Average Talker

Posts: 16
Trades: 0
Thanks for the advice guys. Any chance of pointing me in the direction of some samples so I can work out where to start!

Thanks again,
Dan
superdan_35 is offline
Reply With Quote
View Public Profile
 
Old 03-24-2009, 10:56 AM Re: Dynamic Form
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I dont know how much of either php or JS you know already. But assuming you can already set up a database connection and retrieve data etc. the only hard part is really in the usage of jQuery and combining it with php, which is actually rather easy.

You set an id to the drop down field (<select> tag), and then you can add handlers to it with jQuery, create function which will execute att certain events. Something like

Code:
<script language="javascript">
// This writing is standard and can be found on jQuery's site
$(document).ready(function(){
   $('#you_select_id').select(function(){
      // stuff to do on select value
      // like using get or post (functions in jQuery) to call a php page,
      // which will then retrieve stuff from the database and return it.
      // You can then use that data here, in this function.

      // Btw, to "return" data from the php page to the get/post functions
      // you simply print them out. The whole string will be returned.
      // You can also use json format to return variables, arrays etc.
      // php already has functions for encoding/decoding json format
   });
});
</script>
I know this isn't much but you wont need much more from me. There are tutorials and lots of examples/free scripts on jQuery's website, http://jquery.com/
I went through their beginners tutorial and used it as a reference, together with their acrticles and examples, and had little problems putting together all my scripts.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-24-2009, 12:44 PM Re: Dynamic Form
Average Talker

Posts: 16
Trades: 0
Hi again and thanks for the quick reply. I have had a look at your code and jQuery and I understand what is happening but can't seem to apply it to my problem.

I currently have the following for my form:

Code:
// Show drop down of staff members
<select name="staffname" onchange="showStaff(this)">
$i = 0;
while ($i < $num)
{
     echo '<OPTION value='.$assos_id[$i].'> '.$staff[$i].'';
     $i++;
}    
</select>

// Run query based on selection
$query = @mysql_query("SELECT * FROM staff WHERE staff_id LIKE $staff_id");
$row = mysql_fetch_object($query);

// Show results
First Name: <input type="text" name="fname" value="'.$row->fname.'"/>
Surname: <input type="text" name="lname" value="'.$row->lname.'"/>
// And do on
The JavaScript function has been created to show it works but has no relevant functionality:
Code:
<script language="javascript">
function showStaff(choice) 
{
    var choice = choice.options[choice.selectedIndex].value;
    alert (choice);
}
</script>
So how do I manipulate the php in the function and send it back? Or in otherwords, how do I get choices from the JavaScript to be staff_id in the php?

Thanks again,
Dan
superdan_35 is offline
Reply With Quote
View Public Profile
 
Old 03-25-2009, 12:45 AM Re: Dynamic Form
dark_lord's Avatar
Experienced Talker

Posts: 41
Name: Parijat Roy
Location: INDIA-KOLKATA
Trades: 0
hi superdan_35

let us say you have a page called staff.php where you have this code

PHP Code:
<?php
// Show drop down of staff members
<select name="staffname" onchange="showStaff(this)">
$i 0;
while (
$i $num)
{
     echo 
'<option value=\"'.$assos_id[$i].'\"> '.$staff[$i].'</option>';
     
$i++;
}    
</
select>

// Run query based on selection
$staff_id $_GET["staff_id"];
if(isset(
$staff_id))
{
$query = @mysql_query("SELECT * FROM staff WHERE staff_id LIKE $staff_id");
$row mysql_fetch_object($query);

// Show results
echo "First Name: <input type=\"text\" name=\"fname\" value=\"'.$row->fname.'\"/>
Surname: <input type=\"text\" name=\"lname\" value=\"'.
$row->lname.'\"/>";
// And do on
}
and change that JavaScript function as
Code:
<script language="javascript">
function showStaff(choice) 
{
    var choice = choice.options[choice.selectedIndex].value;
    document.location.href = "staff.php?staff_id="+choice;
}
</script>
the logic is whenever someone change the drop down lists, the whole page will get refresh and if staff_id is set, then it will show the corresponding staff information
__________________
I AM THE BEAUTIFUL NIGHTMARE

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
dark_lord is offline
Reply With Quote
View Public Profile Visit dark_lord's homepage!
 
Old 03-25-2009, 01:06 AM Re: Dynamic Form
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Try this
Code:

<script>
$(document).ready(function(){   $('#drop_down_id').select(function(){
      var input = $('#drop_down_id').val();
      // The post method looks like $.post(url, data, callback, type);
      $.post('get_data.php', {selectedValue : val}, function(response){
         alert(response);
      });
   });
});
</script>
The file get_data.php will now get the selected value from the drop down box (from $_POST['selectedValue']). Use it to retrive whatever data you need and print it out to the page. Just to try it out, make the page only print out "Hello World!", the text is then returned to the callback function as the parameter response, and shown with alert(). This happens every time you select something from the drop down.
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 03-25-2009, 06:00 AM Re: Dynamic Form
Average Talker

Posts: 16
Trades: 0
Just tried dark_lords code and it is working. Needs a bit of polishing but we are there. Thanks very much all.

Dan
superdan_35 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Dynamic Form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29276 seconds with 12 queries