Solved

Code help. Reload to same page when hit submit button

Posted on 2008-10-02
5
1,636 Views
Last Modified: 2013-12-12
I have the following code. When I click Post after editing it takes me back to the list view. How can I just stay at the same page.

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">http://www.sqlmaestro.com/products/mysql/phpgenerator/</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>

<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
5 Comments
 
LVL 4

Expert Comment

by:kingmonty
Comment Utility
change your form action from action="test.php" to action="<?php echo $PHP_SELF; ?>"
0
 

Assisted Solution

by:expermedia
expermedia earned 100 total points
Comment Utility
You just need to change line no. 168 to
<form enctype="multipart/form-data" action="" method="post">
0
 
LVL 9

Author Comment

by:syedasimmeesaq
Comment Utility
hey guys I tried your solutions but they didn't work.
the problem was that I had to enter the recid to stay on the current view. this is what I did
<form enctype="multipart/form-data" action="test.php?a=edit&recid=<?php echo $recid; ?>" method="post">
that worked. However now I want to add an anchore to it so the page stays at same place... anyone know how to do that?
Thanks
0
 
LVL 4

Accepted Solution

by:
kingmonty earned 200 total points
Comment Utility
You can append a named anchor to the end of your URL

so,

<form enctype="multipart/form-data" action="test.php?a=edit&recid=<?php echo $recid; ?>#anchor"method="post">
should do the trick.
0
 
LVL 3

Assisted Solution

by:icarey
icarey earned 200 total points
Comment Utility
This is what I use to send data to the current page

<form action="<?php echo $_SERVER[PHP_SELF];?>" method="post">
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now