Hi guys, I have the following code which works well.

The code is to display all the data from the database in input text field format. User is able to edit and update the details by clicking the update button at the end of each row to update the specific row.

Instead of clicking the update button one by one (if user want to edit and update more than one row), now I would like to create a hyperlink which will update ALL the data at once.

The problem now is I dunno how to pass the value of $_post to the "update.php" since I already have

<form name="form1" action="submitAction.php" method="post">

I am not sure is it possible to do so. Or is there any other alternative ways?

Thanks in advanced! :)

<html>
<script language="javascript" >
<!-- hide
function submitRequest(id) {
document.forms[id].submit();
}
// end hide -->
</script>

<!-- The Javasript (Onclick) to Remove the Readonly Attribute-->
<script language="JavaScript">
  function removeAlignment(id){
   document.getElementById("ProjectName_"+id).removeAttribute("readonly",0);
   document.getElementById("DeviceType_"+id).removeAttribute("readonly",0);
  }
</script>

<body>
<?php

$counter=1;
//Connecting and Accessing the Database 
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("Project", $con);

$result = mysql_query("SELECT * FROM Project where status='Ongoing'");

?>

<p><h2 align="center">ADC Project Funnel</h2></p>
<table border="1" bordercolor="lavender" cellspacing="0" cellpadding="0">
  <tr>
    <td width="20" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">No</font></b></td>
    <td width="78" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Project
      Name</font></b></td>
    <td width="72" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Device Type</font></b></td>
    <td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Status</font></b></td>
    <td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial"></font></b></td>
  </tr>
  <tr>
  <td colspan="15" height="15" bgcolor="#AFEEEE"><font face="Arial" size="1.9">Current Project Assignment</font></td>
  </tr>
  
  <!-- Records from the database (Current Project Assignment) -->
  <?php 
  $i = 0;
    while ($row=mysql_fetch_array($result)){
  ?>
    <tr>
    <form name="form1" action="submitAction.php" method="post">
    <td height="5" width="20"  align="center" style="font-size: 13" valign="middle"><?php echo $counter; ?></td>
    <td height="5" width="72" ><input type="text" autocomplete=off readonly="readonly" id="ProjectName_<?php echo $i; ?>" name="ProjectName<?php echo $row['No'];?>" value="<?php echo  $row['ProjectName'];?>" size="20" style="font-size: 10"></font></td>
    <td height="5" width="72" ><input type="text" autocomplete=off readonly="readonly" id="DeviceType_<?php echo $i; ?>" name="DeviceType<?php echo $row['No'];?>" value="<?php echo  $row['DeviceType'];?>" size="15" style="font-size: 10"></font></td>
    <td height="5" width="67" style="font-size: 13">
    <select name="action" onchange="submitRequest(<?php echo $i; ?>);">
    <option value=>Ongoing </option>
    <option value="<?php echo $row['ProjectName'];?>">Complete</option>
    <option value="<?php echo $row['Flag2'];?>">Future</option>
    <option value="<?php echo $row['Flag1'];?>">Cancel</option>
    <option value="<?php echo $row['No'];?>">Update</option>
    <option value="<?php echo $row['Flag3'];?>">Delete</option>
    </select>								    
    </td>
    <td height="5" width="64" ><input type="button" style="width:100%" value="Edit" onclick="removeAlignment(<?php echo $i; ?>);"></td>
    </tr>
    </form> 
    <?php 
  
    ?>
       
  <?php 
  $i++;
  $counter++;
  }
  ?>   
      

  <tr>
  <td colspan="16" bgcolor="#AFEEEE"><font face="Arial" size="1.9">Add New Project Assignment</font></td>
  </tr>
  
   <!-- Add New Records -->
  <tr>
   <form action="project_insert.php" method="post">
     <td width="20" ></td>
     <td width="78" ><input type="text" autocomplete=off name="ProjectName"  size="40" style="font-size: 10"></font></td>
     <td width="72" ><input type="text" autocomplete=off name="DeviceType" size="15" style="font-size: 10"></font>
     <td width="80" style="font-size: 13">
     	 <select name="Status" style="width:100%">
     	 	<option value=Future>Future</option>
     	 	<option value=Ongoing>Ongoing</option> 	      
	     </select>	    
	   </td>
	   <td>
	   	<input type="Submit" style="width:100%"value="Add">
	   	
	   </td>
	 </form>
  </tr>
</table>
<br/>

<table border="0" align="center" cellpadding="0" cellspacing="10">
	<tr>
         <td valign="middle"><a href="http://localhost/Project/update.php">update</a></td>
 
  </tr>
</table>

<br/>
</body>
</html>

Recommended Answers

All 11 Replies

It is possible same form and one action page by using complex javascript coding.
You need to learn html element arrays
1) like Naming html elements using xyz[],
2) Adding /deleting table rows using javascript code.
3) accessing posted array in php and all that.

If updating all at once is not mandatory then phpdatagrid 4.3.1 (free) is good tool for updating table data.
This tools handles all opertion (add/edit/delete/copy). You just have to intialize the tool object by required parameters

Hi,
what you could do is create a hyperlink called for example Update All.

Considering that your submit button is already pointing to "update.php" script and it has values for $_POST, you need some other way to send it to the same script.

What I would do is next:

HTML HREF:

<a href="locationOfUpdateScript/update.php?action=updateAll">Update All</a>

This way when you click that link, "key => value" pair = "updateAll", is sent to $_GET of your update.php script.

So now, to access that information that was sent , you check in the update.php script if there is $_GET. If there is , it means, that "Update All" link was pressed, and if it is not set, it means something other pointing to that script was pressed.

At the beginning of update.php script use:

if(isset($_GET['action']))
{
      if($_GET['action'] == "updateAll")
      {
             //code for showing and editing entire output
      }
}

//rest of the code is your code for $_POST data send by submit button..

It is possible same form and one action page by using complex javascript coding.
You need to learn html element arrays
1) like Naming html elements using xyz[],
2) Adding /deleting table rows using javascript code.
3) accessing posted array in php and all that.

If updating all at once is not mandatory then phpdatagrid 4.3.1 (free) is good tool for updating table data.
This tools handles all opertion (add/edit/delete/copy). You just have to intialize the tool object by required parameters

Updating all at once is mandatory. Would you show me some of the examples?

Im relatively new to javascript. Thanks!

I do not have such demo code right now. but I will post it when I manage to make a demo.

Expanding on tnjiric response:

add javascript function to top of page somewhere
function updateAll(id) {
     document.form1.setAttribute("action",'submitAction.php?action=updateAll');
     document.form1.submit();
}
line 46 or so in your code:
<td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Update</font></b></td>
line 101 or so in your code (after edit button)
		<td height="5" width="64" ><input type="button" style="width:100%" value="Update All" onclick="updateAll(this);"></td>
also, be careful where you put your form tags.  put them outside of your while loop.
move line 55 (<form name=form1 ...>)
to line 52 and push everything else down a line.  also move your </form> outside of the while loop as well.
so <form>
while code {
}
</form>
change your submitAction.php to this:
<?php
	// if the action is set look for it = to "updateAll"
    if(isset($_GET['action'])) {
		if($_GET['action'] == "updateAll") {
			//code for showing and editing entire output
			echo "action == updateAll, check post<br>";
			//print_r($_POST);
			foreach($_POST as $key => $value) {
				if ($key != 'action') {
					echo "in foreach key = " . $key . " and val = " . $value . "<BR>";
				}
				
			}				
		}
	//  else , the post coming in should be a normal one for a single item, 
    } else {
    // so process the rest of the code is your code for $_POST data send by 'regular' submit button..
	// put your current update code here.
		echo "made the else update called on single record";
    }
?>  // eof

Hi, the code is not working if i put the form tag outside my while loop. Any ideas? =)

Expanding on tnjiric response:

add javascript function to top of page somewhere
function updateAll(id) {
     document.form1.setAttribute("action",'submitAction.php?action=updateAll');
     document.form1.submit();
}
line 46 or so in your code:
<td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Update</font></b></td>
line 101 or so in your code (after edit button)
		<td height="5" width="64" ><input type="button" style="width:100%" value="Update All" onclick="updateAll(this);"></td>
also, be careful where you put your form tags.  put them outside of your while loop.
move line 55 (<form name=form1 ...>)
to line 52 and push everything else down a line.  also move your </form> outside of the while loop as well.
so <form>
while code {
}
</form>
change your submitAction.php to this:
<?php
	// if the action is set look for it = to "updateAll"
    if(isset($_GET['action'])) {
		if($_GET['action'] == "updateAll") {
			//code for showing and editing entire output
			echo "action == updateAll, check post<br>";
			//print_r($_POST);
			foreach($_POST as $key => $value) {
				if ($key != 'action') {
					echo "in foreach key = " . $key . " and val = " . $value . "<BR>";
				}
				
			}				
		}
	//  else , the post coming in should be a normal one for a single item, 
    } else {
    // so process the rest of the code is your code for $_POST data send by 'regular' submit button..
	// put your current update code here.
		echo "made the else update called on single record";
    }
?>  // eof

well, it should as the javascript is pointing to 'form1'. also, if you have the form declaration in your while loop, you are actually creating a 'form1' for as many records as you have. you should view the page source, you should have a minimal amount of forms on your page.
I've also tested this code based on your original example, so what part is not working? debug on your submit.php page

print_r($_POST);  // will print out all post variables
print_r($_GET);  // will print out all get variables
print_r($_REQUEST);  // will print out all request variables

are you getting any values at all, errors?

Erm, I followed what you had suggested.. But it doesnt work.

<form name="form1" action="submitAction.php" method="post">
 <?php 
  $i = 0;
    while ($row=mysql_fetch_array($result)){
  ?>
    <tr>
    
    <td height="5" width="20"  align="center" style="font-size: 13" valign="middle"><?php echo $counter; ?></td>
    <td height="5" width="72" ><input type="text" autocomplete=off readonly="readonly" id="ProjectName_<?php echo $i; ?>" name="ProjectName<?php echo $row['No'];?>" value="<?php echo  $row['ProjectName'];?>" size="20" style="font-size: 10"></font></td>
    <td height="5" width="72" ><input type="text" autocomplete=off readonly="readonly" id="DeviceType_<?php echo $i; ?>" name="DeviceType<?php echo $row['No'];?>" value="<?php echo  $row['DeviceType'];?>" size="15" style="font-size: 10"></font></td>
    <td height="5" width="67" style="font-size: 13">
    <select name="action" onchange="submitRequest(<?php echo $i; ?>);">
    <option value=>Ongoing </option>
    <option value="<?php echo $row['ProjectName'];?>">Complete</option>
    <option value="<?php echo $row['Flag2'];?>">Future</option>
    <option value="<?php echo $row['Flag1'];?>">Cancel</option>
    <option value="<?php echo $row['No'];?>">Update</option>
    <option value="<?php echo $row['Flag3'];?>">Delete</option>
    </select>								    
    </td>
    <td height="5" width="64" ><input type="button" style="width:100%" value="Edit" onclick="removeAlignment(<?php echo $i; ?>);"></td>
    </tr>

       
  <?php 
  $i++;
  $counter++;
  }
  ?> 
    </form>

well, it should as the javascript is pointing to 'form1'. also, if you have the form declaration in your while loop, you are actually creating a 'form1' for as many records as you have. you should view the page source, you should have a minimal amount of forms on your page.
I've also tested this code based on your original example, so what part is not working? debug on your submit.php page

print_r($_POST);  // will print out all post variables
print_r($_GET);  // will print out all get variables
print_r($_REQUEST);  // will print out all request variables

are you getting any values at all, errors?

Please re-read post from 4 days ago.
I modified wrote and tested the code, it does work as described.
so lets try again:

//add javascript function to top of page somewhere
function updateAll(id) {
    document.form1.setAttribute("action",'submitAction.php?action=updateAll');
    document.form1.submit();
}
//line 46 or so in your code:
<td width="67" bgcolor="steelblue" height="15" style="font-size: 11" align="center"><b><font face="Arial">Update</font></b></td>
// This will call submitAction.php
// on line 101 or so in your code (after the edit button) add this line:
<td height="5" width="64" ><input type="button" style="width:100%" value="Update All" onclick="updateAll(this);"></td>

// That will call submitAction.php?action=updateAll

change your submitAction.php to this:
<?php
// if the action is set look for it = to "updateAll"
if(isset($_GET['action'])) {
  if($_GET['action'] == "updateAll") {
  //code for showing and editing entire output
  echo "action == updateAll, check post<br>";
  //print_r($_POST);
  foreach($_POST as $key => $value) {
    if ($key != 'action') {
      echo "in foreach key = " . $key . " and val = " . $value . "<BR>";
    }
 
  }
}
// else , the post coming in should be a normal one for a single item,
} else {
// so process the rest of the code 
is your code for $_POST data send by 'regular' submit button?
// put your current update code here.
echo "made the else update called on single record";
}
?> // eof

Sorry to inform you chaychie but ddymaceks code works perfectly fine , and his logic is absolutely correct, so the coding is not the problem.

make a checkbox then assign id to each value in a row, just name your checkbox to array, example

<form>
<input type="checkbox" name="cid[]" value="1" /> 1<br />
<input type="checkbox" name="cid[]" value="2" /> 2<br />
<input type="submit" name="submit" value="submit" />
</form>

replace value with your id in your database. when you submit the form, values are stored in an array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.