Jump to content

send user back to the previous URL


wkilc

Recommended Posts

Hello,

 

I have a form that allows users to change to particular value in a database.

 

You can query the page... for example, this URL will display only the student records that are "Flute":

index.php?instrument=Flute

 

Here's my question, if folks use the form and update a record, it takes currently them back to "index.php?p=index" after the update...

 

How can I "trap" the URL they were just looking at, sending them back to the same "queried" page they were just on, showing the same records that were just looking at?  I guess it would be almost like using the "back button".  It would need to be dynamic... it might be "index.php?instrument=Flute", or "index.php?instrument=Violin"

 

Here's the form code.

 

 

<form action="index.php?p=index" method="post" name="editstudent">
  <b>Name</b>: Kent, Clark  
<br />
     <b>Instrument</b>: Winds-Flute 
        <br />
        <b>Rank</b>: <input name="rank" type="text" id="rank" size="7" value="2" />

        <br />
        <br />
        <input type="hidden" name="id" value="5" />
        <input type="submit" value="Update" name="updatestudentbt" />
      </p>
  </form>
<br />
<a href="index.php?p=index">Go back</a>

 

TIA for any help,

 

~Wayne

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/111355-send-user-back-to-the-previous-url/
Share on other sites

You are not totally clear. The code you posted, are it in the page index.php?instrument=Flute? Then you can simply change your form action. Now if it's not, then store the previous page in a session variable and then use header() to route that page once the processing is done.

Thank you for the replies...

 

I tried this... doesn't work.

<form action="history.go(-1)" method="post" name="editstudent">

 

The form IS on the same page, I believe... I didn't write it.  I'm a newbie, if you haven't guessed. :-[

 

Here's the complete code, sorry for pasting so much:

 

 

 <?
    if (isset($_POST['updatestudentbt']))
{
	if ($_POST['rank'] != '')
	{
		if (mysql_query("UPDATE bocj SET `rank` = '".$_POST['rank']."' WHERE `id`= '".$_POST['id']."'"))
			echo "Student was successfully updated!<br /><br />";
		else
			echo "Student was NOT updated!<br /><br />";
	}

}

    if ($_GET['a'] == "cdelete")
    {
	mysql_query("DELETE FROM bocj WHERE id='".$_GET['id']."'");
}


if ($_GET['a'] == "edit")
    {
	$sql = "SELECT * FROM bocj WHERE id='".$_GET['id']."'";
    	$result = mysql_query($sql);
    	$data = mysql_fetch_array($result);
?>
    <form action="index.php?p=index" method="post" name="editstudent">
     <b>Name</b>: <? echo $data['student']?>  
<br />
     <b>Instrument</b>: <?=$data['instrument']?>
        <br />
        <br />
        <b>Rank</b>: <input name="rank" type="text" id="rank" size="7" value="<? echo $data['rank']?>" />
        <br />
        <br />
        <input type="hidden" name="id" value="<? echo $data['id']?>" />
        <input type="submit" value="Update" name="updatestudentbt" />
      </p>
  </form>
<br />
<a href="index.php?p=index">Go back</a>
<?
}
    else
    {
    ?>


<?
  function pparams($sort) {
    global $sponsor, $school, $student, $instrument;

    echo $HTTP_SERVER_VARS['PHP_SELF'];
    echo "?sponsor=".htmlentities(urlencode($sponsor));
    echo "&student=".htmlentities(urlencode($student));
    echo "&instrument=".htmlentities(urlencode($instrument));
    echo "&sort=".htmlentities(urlencode($sort));
  }

  if (!$sort) $sort="instrument";
?>


      	<table>
      	<tr>
        <td><strong><a href="<? pparams("sponsor"); ?>">Sponsor</a></strong></td>
        <td><strong><a href="<? pparams("student"); ?>">Student</a></strong></td>
<td><strong><a href="<? pparams("instrument"); ?>">Instrument</a></strong></td></td>
<td><strong><a href="<? pparams("rank"); ?>">Rank</a></strong></td>
    	</tr>
       	<?
    	$sql = "SELECT bocj.sponsor,bocj.student,bocj.instrument,bocj.rank,bocj.id
        FROM bocj
        WHERE ((bocj.sponsor LIKE '$sponsor%')
              	and (bocj.student LIKE '$student%')
   		and (bocj.instrument LIKE '$instrument%')
   		and (bocj.rank LIKE '$rank%')
   		and (bocj.id LIKE '$id%')
        ORDER BY bocj.$sort";
    	$result = mysql_query($sql);
        while ($data = mysql_fetch_array($result))

    	{
    	?>
    	<tr>
<td><?=$data['sponsor']?></td>
<td><?=$data['student']?></td>
<td><?=$data['instrument']?></td>
<td><a href="index.php?p=index&a=edit&id=<?=$data['id']?>"><?=$data['rank']?></a>
	</td>
	</tr>
        <?}?>
    </table>
<? } ?>

 

~Wayne

 

 

try this:

 

<form action="index.php?p=index" method="post" name="editstudent">
  <b>Name</b>: Kent, Clark  
<br />
     <b>Instrument</b>: Winds-Flute 
        <br />
        <b>Rank</b>: <input name="rank" type="text" id="rank" size="7" value="2" />

        <br />
        <br />
        <input type="hidden" name="id" value="5" />
        <input type="submit" value="Update" name="updatestudentbt" />
<input type="hidden" name= "goto" value="<?php echo $_SERVER["PHP_SELF"]."?instrument=".$_GET['instrument']; ?>" />
      </p>
  </form>
<br />
<a href="index.php?p=index">Go back</a>

 

then on your index.php redirect them back by adding this after you have finished processing the rest of the form:

 

<?php
$goto=$_POST['goto']
header ("location: $goto");
?>

Thanks once again  for the help.  ;D

 

I've changed my method... it works, but it doesn't.

 

 

Here's a test page:

http://www.menc-eastern.org/test.php

 

You see 5 records.

 

Now "query" the database:

http://www.menc-eastern.org/test.php?instrument=Winds-Flute

 

Just two records, flute ones... so far, so good.

 

Now click on one of the hyperlinked "ranks"... change the rank number and Update the value.

 

It displays "Student was successfully updated!"... and it goes back to the URL I wanted

http://www.menc-eastern.org/test.php?instrument=Winds-Flute

 

... but there is only one student listed there (the one that was just updated).  Copy and paste the EXACT same URL in a new tab, and it displays both students.

 

How do I make it display all (in this case, both) students, immediately after the update?

 

~Wayne

 

 

Here's my latest code:

 

<? $foobar = getenv('HTTP_REFERER'); ?>

<?
    if (isset($_POST['updatestudentbt']))
{
	if ($_POST['rank'] != '')
	{
		if (mysql_query("UPDATE bocj SET `rank` = '".$_POST['rank']."' WHERE `id`= '".$_POST['id']."'"))
			echo "Student was successfully updated!<br /><br />";
		else
			echo "Student was NOT updated!<br /><br />";
	}

}

if ($_GET['a'] == "edit")
    {
	$sql = "SELECT * FROM bocj WHERE id='".$_GET['id']."'";
    	$result = mysql_query($sql);
    	$data = mysql_fetch_array($result);
?>


    <form action="<? echo $foobar?>" method="post" name="editstudent">
     <b>Name</b>: <? echo $data['student']?>  
<br />
     <b>Instrument</b>: <?=$data['instrument']?>
        <br />
        <br />
        <b>Rank</b>: <input name="rank" type="text" id="rank" size="7" value="<? echo $data['rank']?>" onfocus="if(this.value=='rank?') {this.value='';}" onblur="if(this.value=='rank?') {this.value='';}" />
        <br />
        <br />
        <input type="hidden" name="id" value="<? echo $data['id']?>" />
        <input type="submit" value="Update" name="updatestudentbt" />
      </p>
  </form>
<br />

<?
}
    else
    {
    ?>


<?
  function pparams($sort) {
    global $sponsor, $school, $student, $instrument;

    echo $HTTP_SERVER_VARS['PHP_SELF'];
    echo "?sponsor=".htmlentities(urlencode($sponsor));
    echo "&student=".htmlentities(urlencode($student));
    echo "&instrument=".htmlentities(urlencode($instrument));
    echo "&sort=".htmlentities(urlencode($sort));
  }

  if (!$sort) $sort="instrument";
?>


      	<table>
      	<tr>
        <td><strong><a href="<? pparams("sponsor"); ?>">Sponsor</a></strong></td>
        <td><strong><a href="<? pparams("student"); ?>">Student</a></strong></td>
<td><strong><a href="<? pparams("instrument"); ?>">Instrument</a></strong></td></td>
<td><strong><a href="<? pparams("rank"); ?>">Rank</a></strong></td>
<td><strong>Comments</strong></td>
    	</tr>
       	<?
    	$sql = "SELECT bocj.sponsor,bocj.student,bocj.instrument,bocj.rank,bocj.id,bocj.comments
        FROM bocj
        WHERE ((bocj.sponsor LIKE '$sponsor%')
              	and (bocj.student LIKE '$student%')
   		and (bocj.instrument LIKE '$instrument%')
   		and (bocj.rank LIKE '$rank%')
   		and (bocj.id LIKE '$id%')
   		and (bocj.comments LIKE '$comments%'))
        ORDER BY bocj.$sort";
    	$result = mysql_query($sql);
        while ($data = mysql_fetch_array($result))

    	{
    	?>
    	<tr>
<td><?=$data['sponsor']?></td>
<td><?=$data['student']?></td>
<td><?=$data['instrument']?></td>
<td><a href="test.php?p=index&a=edit&id=<?=$data['id']?>"><?=$data['rank']?></a></td>
        <td width="120"><?=$data['comments']?></td>
        </tr>
        <?}?>
    </table>
<? } ?>

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.