5 Replies - 21920 Views - Last Post: 30 July 2010 - 06:32 PM Rate Topic: -----

#1 Sree07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-July 10

How to display values using dynamic textbox and get the values using t

Posted 28 July 2010 - 08:56 AM

Hi..
I'm facing a problem. I have to display a table from the database and display it, and the user can change this textbox value. For this I'm creating a dynamic text boxes to display the value. This displayed information needs to be changed according to the user requirements. designation and its order.

To get the value from the database
 

//to get the database values:

$sql = "SELECT *
FROM `designations` ORDER BY desid";

// displaying it

<input size="5" name="<? $desid[$i] ?>" type="text" value="<? echo $row1["desid"] ?> <? echo $_REQUEST["$desid[$i]"]; ?>">  




Now in the above code the display section works perfectly. The only problem I'm facing is that when the user modifies the values in the textboxes and clicks submit button how should i get the modified values??

I tries using the array name and post method but its not working.. Please help.. :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: How to display values using dynamic textbox and get the values using t

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3105
  • Posts: 10,903
  • Joined: 08-August 08

Re: How to display values using dynamic textbox and get the values using t

Posted 28 July 2010 - 09:06 AM

Because you've posted incomplete code there's no way to know what you've done or where your problem is.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10821
  • View blog
  • Posts: 40,346
  • Joined: 27-December 08

Re: How to display values using dynamic textbox and get the values using t

Posted 28 July 2010 - 09:07 AM

Quote

I tries using the array name and post method but its not working.. Please help..

What exactly are you trying to do? Update records in a database? What isn't working? Can you expand on your problem and show us some more code? GET and POST are the only two methods to send form data to the servers (you might be able to use AJAX with PHP, but I don't know Javascript at all, so I can't recommend that) and process the data.
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: How to display values using dynamic textbox and get the values using t

Posted 28 July 2010 - 12:35 PM

Try generating HTML that looks something like this:
<form action="whatever.php" method="post">
    <input type="text" name="des[0]" value="first"><br>
    <input type="text" name="des[1]" value="second"><br>
    <input type="text" name="des[2]" value="third"><br>
    <input type="submit">
</form>


Where the des[1] in the name reflects the ID of the row inside the database. (Your $desid[$i], if I am not mistaken.)


Then you can loop through them in your PHP code, by using the $_POST['des'] as an array. You could do something along the lines of:
<?php
if(isset($_POST['des'])) 
{
    // Query the original values for those passed.
    $ids = mysql_real_escape_string(implode(", ", array_keys($_POST['des'])));
    $sql = "SELECT desid, value FROM designations WHERE desid IN($ids)";
    $result = mysql_query($sql) or die(mysql_error());
    
    // Loop through and update those that changed.
    while($row = mysql_fetch_assoc($result)) 
    {
        // Match the $row['value'] against the $_POST[$row['desid']] value and
        // update the table if they aren't the same.
        // (I'll leave that part to you.)
    }
}
?>


Was This Post Helpful? 0
  • +
  • -

#5 Sree07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-July 10

Re: How to display values using dynamic textbox and get the values using t

Posted 30 July 2010 - 09:31 AM

Sorry for posting incomplete code.
The thing i'm trying to do is to display the designation name and its order from the database. The number of designations will vary so i cant fix a specific number thats the problem.

I'm displaying the designation order in a textbox so that the user can change the order of the designation.

The below code works good for displaying but i don't know how to give the name for textboxes so that their values could be accessed when the user clicks the update button. Hope i explaied it clearly this time.. please help out by giving your valuable suggestions.. Thanks in advance.

<?
$sql = "SELECT *
FROM `designations` ORDER BY desid";
$result = mysql_query($sql); 
echo $count;
	while($row1 = mysql_fetch_assoc($result))
{
	$i++;
	 
?>



<table width="30%" align="center">
<tr>
//displaying designation name

<td align="left"><? echo $row1["desname"]  ?>
</td>

//displaying the oder in which the designations needed to be arranged

<td align="right"><input size="5" name="<? $desid[$i] ?>" type="text" value="<? echo $row1["desid"] ?> <? echo $_REQUEST["$desid[$i]"]; ?>">  
</td>
</tr>
<?

}
?>
<?
if($_REQUEST['action']=="update")
{

	//What code should be used in here please help

	
} ?>



Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: How to display values using dynamic textbox and get the values using t

Posted 30 July 2010 - 06:32 PM

Line #23 in the code looks very odd. First of all, where is the $desid array coming from? I don't see it defined anywhere.

Second. This: <? $desid[$i] ?> is a "effect-less" call. It does nothing; nothing is printed and no value change. If you want to print the value it needs to be either: <?=$desid[$i]?> or <?php echo $desid[$i]; ?>, the latter being the preferred method.

And on that subject, you really should avoid using the <? ... ?> style of code blocks. They are disabled by default, so using them may cause portability problems in the future. You should always try using the full <?php ... ?> syntax instead. It works on all servers and only costs you a few extra keystrokes.

As to your problem. I already posted the method we use to pass arrays of data from forms. If you want your input boxes to carry dynamic names; like the ID of a database row, you can use that method. To allow users to update your data, you basically just need to do the following:
  • Fetch the data from the database. (I'll leave that up to your imagination.)
  • Print input boxes for each row. E.g:
    while($row = mysql_fetch_assoc($result)) {
        echo "<input type=\"text\" name=\"row[{$row['id']}]\" value=\"{$row['value']}\"><br>";
    }
    

  • In the code that receives the data, update each row with the new value.
    foreach($_POST['row'] as $_id => $_value) {
        $_id = (int)$_id;
        $_value = mysql_real_escape_string($_value);
        mysql_query("UPDATE tbl SET value = '{$_value}' WHERE `id` = {$_id} LIMIT 1");
    }
    
    


If this doesn't help, we need more information if we are to give you more targeted answers. How does your database look like? Which parts of it exactly are you trying to have your user change?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1