Keep the current URL parameters in the URL after form submission

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 25939 Views - Last Post: 16 June 2011 - 12:15 AM Rate Topic: -----

#1 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-March 11

Keep the current URL parameters in the URL after form submission

Posted 01 June 2011 - 01:04 PM

Hi

I am building a php webpage that contains a comments section.

The display of the webpage relys on 2 URL parameters ?software_id=2&id=2

Can someone please tell me if there is a way to keep the current URL parameters in the URL
after a form on the webpage has been submitted.

This is my current URL

127.0.0.1/fulltitle.php?software_id=2&id=2

This is my URL after clicking on submit

127.0.0.1/fulltitle.php

Best regards Maxwell


Is This A Good Question/Topic? 0
  • +

Replies To: Keep the current URL parameters in the URL after form submission

#2 eZACKe  Icon User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Keep the current URL parameters in the URL after form submission

Posted 01 June 2011 - 01:14 PM

So what you're saying is, after submitting the comment you want to make sure the page that the user was on is the one that gets reloaded?


Couldn't you just use $_GET and take advantage of already having the parameters?

For example, in the URL before submitting: 127.0.0.1/fulltitle.php?software_id=2&id=2

You could do:
$software = $_GET['software_id'];
$id = $_GET['id'];

// and then when you submit the form, do something like:
<form method='post' action='fulltitle.php?software_id=<?php $software ?>&id=<?php $id ?>'>



EDIT: Or, actually: best to not even make the new variables. Couldn't you just put $_GET['software_id'], etc. directly into the action part?

Maybe I'm not understanding the question correctly. If so, sorry about that. I believe something like this should work though.

This post has been edited by eZACKe: 01 June 2011 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Keep the current URL parameters in the URL after form submission

Posted 01 June 2011 - 01:17 PM

You can either use $_SERVER['HTTP REFERER'] to get the name of the previous page which isn't very reliable or you can store the URL of every page in a session variable. Then you would read the $_SESSION variable and redirect them to the URL retrieved after they've logged in. Be careful not to loose the $_SESSION variables when you start a new session upon user login.

This post has been edited by codeprada: 01 June 2011 - 01:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Keep the current URL parameters in the URL after form submission

Posted 01 June 2011 - 02:46 PM

An alternative would be to use AJAX and never even move away from the original page.
Was This Post Helpful? 0
  • +
  • -

#5 VolcomMky  Icon User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 315
  • Joined: 13-May 09

Re: Keep the current URL parameters in the URL after form submission

Posted 02 June 2011 - 10:46 AM

You could use AJAX or easier and similar, posting a form to a iframe.

Another idea could be to create a hidden field with the url variables as the value and on the action page redirect to the url + hidden field value.

Look into http_build_query
(http://www.php.net/manual/en/function.http-build-query.php)

Example:
<input type="hidden" name="carry_vars" value="<?=http_build_query($_REQUEST)?>">


or

<input type="hidden" name="redir_to" value="fulltitle.php?<?=http_build_query($_REQUEST)?>">


Good coding practive does not use <?, it is technically correct with <?php

This post has been edited by VolcomMky: 02 June 2011 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-March 11

Re: Keep the current URL parameters in the URL after form submission

Posted 09 June 2011 - 04:31 PM

Hi
Thanks for the help!

I am already using a prepared statement that extracts the software_id URL parameter
and displays my comments section. The form that submits the comment is on the same page.

$hostname = "127.0.0.1";
$username = "root";  
$password = "root";  

try {  

$pdo = new PDO("mysql:host=$hostname;dbname=abe", $username, $password); 

}catch (PDOExeption $e){  

 echo $e->getMessage();  
 }  
$dbh = $pdo->prepare("SELECT `date`, `name`, `email`, `comment`, `software_id` FROM `guest` WHERE `software_id` = ? ORDER BY `date` DESC"); 

$dbh->bindValue(1, $_GET['software_id'], PDO::PARAM_INT); // assuming it's an integer    

$dbh->execute();       

// fetch data    

$dbh->setFetchMode(PDO::FETCH_ASSOC); 

while($row = $dbh->fetch()) {echo '<table width="100%" cellspacing="0" cellpadding="3" class="tablebg">
        <tr class="formlabels">
          <td align="left" class="tl" width="40%">Author:&nbsp;'.$row['name'].'</td>
          <td width="45%" align="left">Date:&nbsp;'.$row['date'].'</td>
          <td width="15%" align="center" class="tr"><a href="mailto:'.$row['email'].'">Email Author</a></td>
          </tr>
        <tr class="formlabels">
          <td colspan="4" class="bl">'.$row['comment'].'</td>
          </tr>
      </table>';
	  }
?>



I tried adding the following to my form action

<form method='post' action='fulltitle.php?software_id=<?php $software ?>&id=<?php $id ?>'>


I also created the two variables

$software = $_GET['software_id'];  
$id = $_GET['id']; 


When I click submit I now get

127.0.0.1/abe/fulltitle.php?software_id=&id=

Is there possibly a way to access the software_id=&id= URL parameters using another prepared statement?

Many thanks Maxwell






View PostVolcomMky, on 02 June 2011 - 10:46 AM, said:

You could use AJAX or easier and similar, posting a form to a iframe.

Another idea could be to create a hidden field with the url variables as the value and on the action page redirect to the url + hidden field value.

Look into http_build_query
(http://www.php.net/manual/en/function.http-build-query.php)

Example:
<input type="hidden" name="carry_vars" value="<?=http_build_query($_REQUEST)?>">


or

<input type="hidden" name="redir_to" value="fulltitle.php?<?=http_build_query($_REQUEST)?>">


Good coding practive does not use <?, it is technically correct with <?php

Was This Post Helpful? 0
  • +
  • -

#7 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Keep the current URL parameters in the URL after form submission

Posted 09 June 2011 - 05:04 PM

The variables $software and $id aren't defined. You've got them defined as coming from $_GET vars, but that's not working, so my guess is you're either not submitting your form via GET, or the field names are wrong. If you want to post your form code, I could say with more certainty.

Or you can print_r($_GET) to see what the GET array looks like.
Was This Post Helpful? 0
  • +
  • -

#8 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-March 11

Re: Keep the current URL parameters in the URL after form submission

Posted 10 June 2011 - 03:24 AM

I added var_dump($_GET); exit; to my comments page.
When the page that contains the comments page loads I get this -

array(2) { ["software_id"]=> string(1) "3" ["id"]=> string(1) "3" }

When I submit the comments form I get this

array(2) { ["software_id"]=> string(0) "" ["id"]=> string(0) "" }


I tried changing the method to get, which resulted in the contents of all of the form fields showing as parameters.

127.0.0.1/abe/fulltitle.php?name=Michael&[email protected]&comment=test+comment&software_id=&submit=Submit

This is the full code for the comments page -

$hostname = "127.0.0.1";
$username = "root";  
$password = "root";  

try {  

$pdo = new PDO("mysql:host=$hostname;dbname=abe", $username, $password); 

}catch (PDOExeption $e){  

 echo $e->getMessage();  
 }  
$dbh = $pdo->prepare("SELECT `date`, `name`, `email`, `comment`, `software_id` FROM `guest` WHERE `software_id` = ? ORDER BY `date` DESC"); 

$dbh->bindValue(1, $_GET['software_id'], PDO::PARAM_INT); // assuming it's an integer    

$dbh->execute();       

 
$software = $_GET['software_id'];  
$id = $_GET['id'];


$dbh->setFetchMode(PDO::FETCH_ASSOC); 

while($row = $dbh->fetch()) {echo '<table width="100%" cellspacing="0" cellpadding="3" class="tablebg">
        <tr class="formlabels">
          <td align="left" class="tl" width="40%">Author:&nbsp;'.$row['name'].'</td>
          <td width="45%" align="left">Date:&nbsp;'.$row['date'].'</td>
          <td width="15%" align="center" class="tr"><a href="mailto:'.$row['email'].'">Email Author</a></td>
          </tr>
        <tr class="formlabels">
          <td colspan="4" class="bl">'.$row['comment'].'</td>
          </tr>
      </table>';
	  }
?>
<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
</head>
<body>
<hr />
<form method='post' action='fulltitle.php?software_id=<?php $software ?>&id=<?php $id ?>'> 
<table align="center">
<tr><td colspan="2" align="center">
<p>Post a comment</p></td></tr>
<tr><td align="right" class="formlabels">Name:</td><td>
    <input type="text" name="name" maxlength="25" /></td></tr>
    <tr><td align="right" class="formlabels">Email:</td><td><input type="text" name="email" maxlength="150" /></td></tr><tr><td align="right" valign="top" class="formlabels">Comment:</td><td>
    <textarea name="comment" cols="30" rows="6" maxlength="300"></textarea></td></tr><tr>
      <td><input type="hidden" name="software_id" id="software_id" value=""></td><td>
    <input type="submit" name="submit" value="Submit"></td></tr>
</table>
    </form><br />
    <br /><br />
</body>
</html> 






View Postsatis, on 09 June 2011 - 05:04 PM, said:

The variables $software and $id aren't defined. You've got them defined as coming from $_GET vars, but that's not working, so my guess is you're either not submitting your form via GET, or the field names are wrong. If you want to post your form code, I could say with more certainty.

Or you can print_r($_GET) to see what the GET array looks like.

This post has been edited by [email protected]: 10 June 2011 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#9 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Keep the current URL parameters in the URL after form submission

Posted 10 June 2011 - 04:16 AM

Oh, you're posting your vars from your form, so to access them you need to use $_POST['software'] and $_POST['id']. Pardon me if I've lost the gist of this message... are you trying to take these vars (either GET or POST) and make them all GET or something?

Anyway, to do something like that you could do:

$software_id = ($_POST['software_id'])? $_POST['software_id'] : $_GET['software_id'];




ditto with the other one. Basically the code checks to see if $_POST['software_id'] is defined. If it is, it sets the var to it. If it's not defined, it checks the GET array instead. If neither is defined, it just comes out null. Is that more what you're looking for?
Was This Post Helpful? 0
  • +
  • -

#10 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-March 11

Re: Keep the current URL parameters in the URL after form submission

Posted 10 June 2011 - 09:25 AM

Thanks again.
I really appreciate you help.

I replaced

$software = $_GET['software_id'];
id = $_GET['id'];


with

$software_id = ($_POST['software_id'])? $_POST['software_id'] : $_GET['software_id'];
$id = ($_POST['id'])? $_POST['id'] : $_GET['id'];


in the previous code

When I load the page containing the included form I get

Notice: Undefined index: software_id in C:\apache\htdocs\abe\guest.php on line 27

Notice: Undefined index: id in C:\apache\htdocs\abe\guest.php on line 28


When I submit the form the URL parameters are still empty

127.0.0.1/abe/fulltitle.php?software_id=&id=

and I get one Undefined index -

Notice: Undefined index: id in C:\apache\htdocs\abe\guest.php on line 28

Should I be combining the two variable declorations

$software_id
$id


with isset

Regards Maxwell

Was This Post Helpful? 0
  • +
  • -

#11 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Keep the current URL parameters in the URL after form submission

Posted 10 June 2011 - 10:52 AM

your relying on data that you didn't check was even set yet when using

$software_id = ($_POST['software_id'])? $_POST['software_id'] : $_GET['software_id'];
$id = ($_POST['id'])? $_POST['id'] : $_GET['id'];



The $_GET array is not containing the values your trying to retrieve. Yes the variable ends up null because it had nothing to set it with but with the side effect that you will get errors for relying on unset data.

edit:

also in your previous post your form declaration is this

<form method='post' action='fulltitle.php?software_id=<?php $software ?>&id=<?php $id ?>'> 



your PHP your breaking into is not doing anything with the variables. You need to echo the variables.

something like

<form method='post' action='fulltitle.php?software_id=<?php echo $software; ?>&id=<?php echo $id; ?>'> 



but this will NOT fix the issue with unset variables.

This post has been edited by RPGonzo: 10 June 2011 - 10:56 AM

Was This Post Helpful? 0
  • +
  • -

#12 satis  Icon User is offline

  • D.I.C Head

Reputation: 82
  • View blog
  • Posts: 231
  • Joined: 26-May 11

Re: Keep the current URL parameters in the URL after form submission

Posted 10 June 2011 - 10:56 AM

y'know, there's a small problem in how you're putting the php vars into your form.

Try changing the form line to this:
<form method='post' action='fulltitle.php?software_id=<?php echo $software ?>&id=<?php echo $id ?>'> 


*edit* Gah, beaten to the punch by Gonzo!

This post has been edited by satis: 10 June 2011 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#13 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Keep the current URL parameters in the URL after form submission

Posted 10 June 2011 - 11:02 AM

hehe oops!

also you can use that method but what I would do is have some default values for your ID's

something like this example

<?php
/**
 * default values
 */
$_def_soft_id = 1;
$_def_id = 9876;

$_soft_id = isset($_GET['software_id']) ? $_GET['software_id'] : $_def_soft_id;
$_id = isset($_GET['id']) ? $_GET['id'] : $_def_id;

$_action = $_SERVER['PHP_SELF'] . '?id=' . $_id . '&amp;software_id=' . $_soft_id;

echo 'Default Values::<br/>';
echo "id=$_def_id <br/>";
echo "software_id=$_def_soft_id <br/><br/>";

echo 'Form Submitted Values (or their defaults)::<br/>';
echo "id=$_id <br/>";
echo "software_id=$_soft_id <br/>";

echo 'Action::<br/>';
echo "form action=$_action <br/>";

echo '$_GET::<br/>';
var_dump($_GET);
echo '<br/>';

echo '$_POST::<br/>';
var_dump($_POST);
echo '<br/>';
?>

<br/><br/><br/><br/>

<form method="post" action="<?php echo $_action; ?>">
	<!-- this can be a hidden input if you like -->
	ID: <input type="text" name="id" value="<?php echo $_id; ?>"/>
		<br/>
	<!-- this can be a hidden input if you like -->
	Software ID: <input type="text" name="software_id" value="<?php echo $_soft_id; ?>"/>
		<br/>
	Test Data 1: <input type="text" name="td_1" value=""/>
		<br/>
	Test Data 2: <input type="text" name="td_2" value=""/>
		<br/>
	Test Data 3: <input type="text" name="td_3" value=""/>
		<br/>
	
		<br/>
	<input type="submit"/>
</form>


This post has been edited by RPGonzo: 10 June 2011 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#14 [email protected]  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-March 11

Re: Keep the current URL parameters in the URL after form submission

Posted 15 June 2011 - 05:08 PM

Hi

Thanks for the help!

I have my comments page almost complete.
The page shows the comments according to software_id, and if I submit the comments form
the page updates to shows the comment.

The fields in the database are id, date, name, email, comment, software_id
The id field is autoincrement so setting it to NULL is ok
the date fields is current_time_stamp so NULL is also ok.

I set software_id to NULL so that the form would submit.
Is there a way to place the current software_id into the database instead of NULL?


This is the code that inserts the comment into the database.


 if(isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $comment = $_POST['comment'];

$mysql = new mysqli('127.0.0.1','root','root','abe') or die('There was a problem connecting to the database');
if($stmt = $mysql->prepare('INSERT INTO guest VALUES (NULL,NULL,?,?,?,NULL)')) {
  $stmt->bind_param('sss',$name,$email,$comment);
  $stmt->execute();
  $stmt->close();
} else {
  echo 'error: ' . $mysql->error;
}
}


The is the full page!

$hostname = "127.0.0.1";
$username = "root";  
$password = "root";  

try {  

$pdo = new PDO("mysql:host=$hostname;dbname=abe", $username, $password); 

}catch (PDOExeption $e){  

 echo $e->getMessage();  
 }  
$dbh = $pdo->prepare("SELECT `date`, `name`, `email`, `comment`, `software_id` FROM `guest` WHERE `software_id` = ? ORDER BY `date` DESC"); 

$dbh->bindValue(1, $_GET['software_id'], PDO::PARAM_INT); // assuming it's an integer    

$dbh->execute();   


$dbh->setFetchMode(PDO::FETCH_ASSOC); 

while($row = $dbh->fetch()) {echo '<table width="100%" cellspacing="0" cellpadding="3" class="tablebg">
        <tr class="formlabels">
          <td align="left" class="tl" width="40%">Author:&nbsp;'.$row['name'].'</td>
          <td width="45%" align="left">Date:&nbsp;'.$row['date'].'</td>
          <td width="15%" align="center" class="tr"><a href="mailto:'.$row['email'].'">Email Author</a></td>
          </tr>
        <tr class="formlabels">
          <td colspan="4" class="bl">'.$row['comment'].'</td>
          </tr>
      </table>';
	  }

 if(isset($_POST['submit'])) {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $comment = $_POST['comment'];

$mysql = new mysqli('127.0.0.1','root','root','abe') or die('There was a problem connecting to the database');
if($stmt = $mysql->prepare('INSERT INTO guest VALUES (NULL,NULL,?,?,?,NULL)')) {
  $stmt->bind_param('sss',$name,$email,$comment);
  $stmt->execute();
  $stmt->close();
} else {
  echo 'error: ' . $mysql->error;
}
}

$_def_soft_id = 1;
$_def_id = 9876;

$_soft_id = isset($_GET['software_id']) ? $_GET['software_id'] : $_def_soft_id;
$_id = isset($_GET['id']) ? $_GET['id'] : $_def_id;

$_action = $_SERVER['PHP_SELF'] . '?id=' . $_id . '&amp;software_id=' . $_soft_id;

echo 'Default Values::<br/>';
echo "id=$_def_id <br/>";
echo "software_id=$_def_soft_id <br/><br/>";

echo 'Form Submitted Values (or their defaults)::<br/>';
echo "id=$_id <br/>";
echo "software_id=$_soft_id <br/>";

echo 'Action::<br/>';
echo "form action=$_action <br/>";

echo '$_GET::<br/>';
var_dump($_GET);
echo '<br/>';

echo '$_POST::<br/>';
var_dump($_POST);
echo '<br/>';
?>

<html>
<head>
<link rel="stylesheet" href="styles.css" type="text/css" media="screen" />
</head>
<body>
<hr />

<br/><br/><br/><br/>

<form method="post" action="<?php echo $_action; ?>">
	<!-- this can be a hidden input if you like -->
	ID: <input type="text" name="id" value="<?php echo $_id; ?>"/>
		<br/>
	<!-- this can be a hidden input if you like -->
	Software ID: <input type="text" name="software_id" value="<?php echo $_soft_id; ?>"/>
		<br/>
	Test Data 1: <input type="text" name="name" value=""/>
		<br/>
	Test Data 2: <input type="text" name="email" value=""/>
		<br/>
	Test Data 3: <input type="text" name="comment" value=""/>
		<br/>
	
		<br/>
	<input type="submit" name="submit" />
</form>
  <?php //var_dump($_GET); exit; ?><br /><br /> -->
</body>
</html>











View PostRPGonzo, on 10 June 2011 - 11:02 AM, said:

hehe oops!

also you can use that method but what I would do is have some default values for your ID's

something like this example

<?php
/**
 * default values
 */
$_def_soft_id = 1;
$_def_id = 9876;

$_soft_id = isset($_GET['software_id']) ? $_GET['software_id'] : $_def_soft_id;
$_id = isset($_GET['id']) ? $_GET['id'] : $_def_id;

$_action = $_SERVER['PHP_SELF'] . '?id=' . $_id . '&amp;software_id=' . $_soft_id;

echo 'Default Values::<br/>';
echo "id=$_def_id <br/>";
echo "software_id=$_def_soft_id <br/><br/>";

echo 'Form Submitted Values (or their defaults)::<br/>';
echo "id=$_id <br/>";
echo "software_id=$_soft_id <br/>";

echo 'Action::<br/>';
echo "form action=$_action <br/>";

echo '$_GET::<br/>';
var_dump($_GET);
echo '<br/>';

echo '$_POST::<br/>';
var_dump($_POST);
echo '<br/>';
?>

<br/><br/><br/><br/>

<form method="post" action="<?php echo $_action; ?>">
	<!-- this can be a hidden input if you like -->
	ID: <input type="text" name="id" value="<?php echo $_id; ?>"/>
		<br/>
	<!-- this can be a hidden input if you like -->
	Software ID: <input type="text" name="software_id" value="<?php echo $_soft_id; ?>"/>
		<br/>
	Test Data 1: <input type="text" name="td_1" value=""/>
		<br/>
	Test Data 2: <input type="text" name="td_2" value=""/>
		<br/>
	Test Data 3: <input type="text" name="td_3" value=""/>
		<br/>
	
		<br/>
	<input type="submit"/>
</form>



Best regards Maxwell


Was This Post Helpful? 0
  • +
  • -

#15 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: Keep the current URL parameters in the URL after form submission

Posted 15 June 2011 - 07:27 PM

If software_id is auto-incremental then you can skip it by specifying the fields you're inserting.

Example (Copy and paste at your own risk)
$pdo->prepare("INSERT INTO `your_table`(`name`, `email`, `other`) VALUES(?, ?, ?)"); //software_id will increment itself


The fields that are set to auto-increment will increment themselves without you having to insert NULL into them every single time.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2