Jump to content

Help with dynamically created buttons and looping statement


Darkmatter5

Recommended Posts

Here's my code:

 

<table width="304" border="0" cellpadding="2" cellspacing="1">
<th colspan="2" class="style1" align="center">CABINET MAINTENANCE</th>
<tr>
<form>
<td class="style2" align="center" valign="top">
<fieldset class="text_boxes"><legend>ADD A CABINET</legend>
<table width="300" border="0" cellpadding="2" cellspacing="1">
<tr>
<td width="70" class="required_text">Name</td>
<td width="230"><input name="cab_name" type="text" class="text_boxes"></td>
</tr>
<tr>
<td>Description</td>
<td><input name="cab_description" type="text" class="text_boxes"></td>
</tr>
</table>
</fieldset>
<fieldset class="text_boxes"><legend>EDIT A CABINET</legend>
<table width="300" border="0" cellpadding="2" cellspacing="1">
<?php
    include 'library/config.inc.php';
    $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbnamemain);
                      
    $query="SELECT cabinet_id, cab_name, cab_description
        FROM $dbnamemain.cabinets
        ORDER BY cab_name ASC";
    $result=mysql_query($query) or die ($query. '<br>' .mysql_error());
    while(list($cabinet_id, $cab_name, $cab_description)=mysql_fetch_array($result, MYSQL_NUM)) {
        if(empty($cab_description)) { $cabinet=$cab_name; }
        else { $cabinet=$cab_name. ": " .$cab_description; }
        echo "<tr>
            <td width='200'>" .substr($cabinet,0,100). "</td>
            <td width='100' align='center'><input name='edit_cab$cabinet_id' type='submit' class='button' value='edit' /><input name='del_cabinet' type='submit' class='button' value='del' /></td>
            </tr>";
    }
                      
    mysql_close($conn);
?>
<tr><td colspan="2" align="center"><hr></td></tr>
<?php
    include 'library/config.inc.php';
    $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
    mysql_select_db($dbnamemain);
                      
    $query="SELECT cabinet_id, cab_name, cab_description
        FROM $dbnamemain.cabinets";
    $result=mysql_query($query) or die ($query. '<br>' .mysql_error());
    while(list($cabinet_id, $cab_name, $cab_description)=mysql_fetch_array($result, MYSQL_NUM)) {
        $cab_button="edit_cab$cabinet_id";
        if(isset($_POST['$cab_button'])) {
            echo "<tr>
            <td width='70' class='text_boxes'>Name</td>
            <td width='230'><input name='cab_name' type='text' class='text_boxes'></td>
            </tr>
            <td>Description</td>
            <td><input name='cab_description' type='text' class='text_boxes'></td>
            </tr>";
        }
    }
                      
    mysql_close($conn);
?>
</table>
</fieldset>
<input name="save_cabinet" type="submit" class="button" value="Submit Cabinet Data" />
<input type="reset" class="button" value="Clear form" />
</td>
</form>
</tr>
</table>

 

The first PHP code works great and makes each edit button have a unique name matching the cabinet_id in the database table.  The second PHP code is suppose to only be run if one of the "edit_cab(cabinet_id)" buttons is pressed.  So an if statement needs to be created for each instance of edit_cab buttons created.  How can I do this?

If you're wanting to dynamically create buttons based on what the user inputs, you'll need to do one of three things:

[*]Create PHP-generated JavaScript that will make your required changes when the user clicks something

[*]Use HTTP request so the page is submitted when the user clicks, then you can use your PHP IF statement to build the other buttons

[*]Use AJAX

 

AJAX would be best, but it's also the most difficult. If you've never used AJAX, check out this tutorial: http://www.w3schools.com/ajax/default.asp

As for the dynamically created buttons, the first section of PHP code already does that.  It'll make buttons for each records in my database.  For instance:

 

EXAMPLE TABLE

Record 1

cabinet_id:1

cab_name:test1

 

Record 2

cabinet_id:2

cab_name:test2

 

The first set of PHP code will create 2 buttons one named edit_cab1 and the other named edit_cab2.  The second set of code needs to create 2 if statements to be executed if edit_cab1 or edit_cab2 are pressed or for however many records and buttons there are.  Any ideas?

I understand that you're creating the first set of buttons with PHP successfully, and you want the next set to be based on what was clicked in the first set. To do that...

If you're wanting to dynamically create buttons based on what the user inputs, you'll need to do one of three things:

[*]Create PHP-generated JavaScript that will make your required changes when the user clicks something

[*]Use HTTP request so the page is submitted when the user clicks, then you can use your PHP IF statement to build the other buttons

[*]Use AJAX

 

AJAX would be best, but it's also the most difficult. If you've never used AJAX, check out this tutorial: http://www.w3schools.com/ajax/default.asp

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.