Solved

form within a form? or another way

Posted on 2008-10-03
13
236 Views
Last Modified: 2013-12-12
I have the following code below that when clicked submit button submits the form and goes to different section. Now I want to have another submit button that when its clicked the form stays on the same page.
The action property of that button should be
<form enctype="multipart/form-data" action="test.php?a=edit&recid=<?php echo $_SESSION["recid"]; ?>" method="post">
The problem is how to make button behave within the same form so it does a totally different thing from the first button.
Please see my complete code below. Thanks

<?php session_start();
 
?>
 
<?php
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;
 
  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;
 
  $sql = @$_POST["sql"];
 
  switch ($sql) {
    case "update":
      sql_update();
      break;
  }
 
  switch ($a) {
    case "edit":
      editrec($recid);
      break;
    default:
      select();
      break;
  }
 
 
  mysql_close($conn);
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>
 
<?php function select()
  {
  global $a;
  global $showrecs;
  global $page;
 
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Table: test</td></tr>
<tr><td>Records shown <?php echo $startrec + 1 ?> - <?php echo $reccount ?> of <?php echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<?php showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<td class="hr">&nbsp;</td>
<td class="hr"><?php echo "ID" ?></td>
</tr>
<?php
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<td class="<?php echo $style ?>"><a href="test.php?a=edit&recid=<?php echo $i ?>">Edit</a></td>
<td class="<?php echo $style ?>"><?php echo htmlspecialchars($row["ID"]) ?></td>
</tr>
<?php
  }
  mysql_free_result($res);
?>
</table>
<br>
<?php showpagenav($page, $pagecount); ?>
<?php } ?>
 
<?php function showroweditor($row, $iseditmode)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><?php echo htmlspecialchars("ID")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="ID" value="<?php echo str_replace('"', '&quot;', trim($row["ID"])) ?>"></td>
</tr>
</table>
<?php } ?>
 
<?php function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<?php if ($page > 1) { ?>
<td><a href="test.php?page=<?php echo $page - 1 ?>">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<?php } ?>
<?php
  global $pagerange;
 
  if ($pagecount > 1) {
 
  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);
 
    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><?php echo $j ?></b></td>
<?php } else { ?>
<td><a href="test.php?page=<?php echo $j ?>"><?php echo $j ?></a></td>
<?php } } } else { ?>
<td><a href="test.php?page=<?php echo $startpage ?>"><?php echo $startpage ."..." .$count ?></a></td>
<?php } } } ?>
<?php if ($page < $pagecount) { ?>
<td>&nbsp;<a href="test.php?page=<?php echo $page + 1 ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<?php } ?>
</tr>
</table>
<?php } ?>
 
<?php function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="test.php">Index Page</a></td>
<?php if ($recid > 0) { ?>
<td><a href="test.php?a=<?php echo $a ?>&recid=<?php echo $recid - 1 ?>">Prior Record</a></td>
<?php } if ($recid < $count - 1) { ?>
<td><a href="test.php?a=<?php echo $a ?>&recid=<?php echo $recid + 1 ?>">Next Record</a></td>
<?php } ?>
</tr>
</table>
<hr size="1" noshade>
<?php } ?>
 
<?php function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<!-- if I copy another form statement here it makes another new form, which is not what I want -->
<form enctype="multipart/form-data" action="test.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xID" value="<?php echo $row["ID"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?php
  mysql_free_result($res);
} ?>
 
<?php function connect()
{
  $conn = mysql_connect("localhost", "user", "password");
  mysql_select_db("mydb");
  return $conn;
}
 
function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}
 
function sqlstr($val)
{
  return str_replace("'", "''", $val);
}
 
function sql_select()
{
  global $conn;
  $sql = "SELECT ID FROM `test`";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}
 
function sql_getrecordcount()
{
  global $conn;
  $sql = "SELECT COUNT(*) FROM `test`";
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}
 
function sql_update()
{
  global $conn;
  global $_POST;
 
  $sql = "update `test` set ID=" .sqlvalue(@$_POST["ID"], false) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
}
function primarykeycondition()
{
  global $_POST;
  $pk = "";
  $pk .= "(ID";
  if (@$_POST["xID"] == "") {
    $pk .= " IS NULL";
  }else{
  $pk .= " = " .sqlvalue(@$_POST["xID"], false);
  };
  $pk .= ")";
  return $pk;
}
 ?>

Open in new window

0
Comment
Question by:syedasimmeesaq
  • 8
  • 5
13 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22635874
you CANNOT have nested forms.

>> Now I want to have another submit button that when its clicked the form stays on the same page.
If you want to remain on the page, then use a type="button" instead of a type="submit"
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22636006
if I use button it doesn't submit the data
thanks
0
 
LVL 82

Expert Comment

by:hielo
ID: 22636118
try:
...
<!-- if I copy another form statement here it makes another new form, which is not what I want -->
<?php
//provide an array that mapst the value of the button to the desired destination
$actionValues=array();
$actionValues['Post']='test.php';
$actionValues['Other Button']='test.php?a=edit&recid=' .$_SESSION["recid"];
?>
 
<form enctype="multipart/form-data" action="<?php echo $actionValues[ $_POST['action'] ];?>" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xID" value="<?php echo $row["ID"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"></p>
<p><input type="submit" name="action" value="Other Button"></p>
</form>
...

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22636525
thanks heilo thats what I wanted. Just one problem though, why doesn't it save data? When I change it to what you suggested it works the way I want it to but doesn't update my table or change data.
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22636588
ok i see what was the problem with update. But now here is the new problem.
$actionValues['Post']='test.php';
$actionValues['Other Button']='test.php?a=edit&recid=' .$_SESSION["recid"];

if the user hit Post and then hit Other Button, the other Button acts like Post button. But then when the user hits Other Button again then it works. Same goes for Post button. User have to hit it twice in order to work. How can we fix it
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22636674
actually its happening with this $actionValues['Other Button']='test.php?a=edit&recid=' .$_SESSION["recid"];
only.

First time Other Button behaves like Post button and then second time it works..why?
0
 
LVL 82

Expert Comment

by:hielo
ID: 22636761
>> the other Button acts like Post button
BOTH buttons are type="submit" so both will actually submit the form. What dictates whether the request should be post or get is the method attribute. I don't know what you are trying to achieve, but both buttons are (CORRECTLY) submitting to the server because they are type=submit. IF anything, you(the programmer) need to determine if you see
$_POST['action']=='Post' and do one thing, but if you see
$_POST['action']=='Other Button' then do something else.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22636810
>.First time Other Button behaves like Post button and then second time it works..why?
I think the problem you are seeing is that initially when the form loads, most likely there is no such thing as $_POST['action'] so:

...action="<?php echo $actionValues[ $_POST['action'] ];?>" ...
 
will give you:
...action=""...
 
 
Thus, you need specify a default value at the very least:
 
...
<!-- if I copy another form statement here it makes another new form, which is not what I want -->
<?php
//provide an array that mapst the value of the button to the desired destination
$actionValues=array();
$actionValues['Post']='test.php';
$actionValues['Other Button']='test.php?a=edit&recid=' .$_SESSION["recid"];
 
//specify the default method here
$method=$actionValues['Post'];
 
//now if you see $_POST['action'], override the default value
if( isset($_POST['action']) )
{
 $method=$_POST['action'];
}
?>
 
<form enctype="multipart/form-data" action="<?php echo $method; ?>" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xID" value="<?php echo $row["ID"] ?>">
<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"></p>
<p><input type="submit" name="action" value="Other Button"></p>
</form>
...

Open in new window

0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22636910
I tried that but now both of them work same as this one
$actionValues['Post']='test.php';
I think I am not understanding it right
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22637040
I did a simplified version of what you said above just to see if it works or not, I still have to hit exit button twice.
<?php
$lurl = "";
if($_POST["action"] =="exit")
{
$lurl = "test.php";
}
if ($_POST["action"] =="save")
{
$lurl = "test.php?a=edit&recid=$recid";
}


 ?>
<form enctype="multipart/form-data" action="<?php echo $lurl; ?>" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xID" value="<?php echo $row["ID"] ?>">

<?php showroweditor($row, true); ?>
<p><input type="submit" name="action" value="Post"><input type="submit" name="action" value="exit"><br>
<?php echo $recid; ?>


not sure why is it happening
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22637075
when first time the page loads and I hove my mouse over the buttons, I see both of the buttons value in the bottom same..thats the reason its happening. They both have this value
test.php?a=edit&recid=$recid
but if I hit exit once and then hover the mouse then the values are different. I know the problem now but I don't know why is it happening and how can I fix it
Thanks
0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 22637115
On that simplied page, when you type the url to your page directly on the browser's address bar, you are NOT submitting anything so both of your if conditions will evaluate to false. Hence $url will remain an empty string. So initially, when the page loads for the first time, if you look at the browser's source code you should see:
<form enctype="multipart/form-data" action="" method="post">

Since action is blank, the browser will submit the request back to the same page. Once you submit once, THEN one of your if clauses will be true and on the next click you go to one of the desired pages.
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 22641514
ok heilo I figured it out. I redirected them after updating. So I did this



function sql_update()
{
  global $conn;
  global $_POST;
 
  $sql = "update `test` set ID=" .sqlvalue(@$_POST["ID"], false) ." where " .primarykeycondition();
  mysql_query($sql, $conn) or die(mysql_error());
if($_post['action']=="exit")
{
header("location: test.php");
exit;
}
}


and that seemed to work. Thanks !
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question