0

I have a php page which gets $page and $user using post method, I also have a button that i want, to open a URL in the same window using $page and $user variables when clicked, to use them with $_GET[] function. i want my URL be like:

http://www.test.com/test.php?page=$page&user=$user

my code is like this:

<?php
$page=$POST_['page'];
$user=$POST_['user'];
<?


<html>
<head>

function openurl() {
var user=<?php echo "$user";?>;
var page=<?php echo "$page";?>;
open('www.test.com/test.php?page='+page'&user='+user,'_self');

}

</head>
<body>

<button onclick="openurl()" type="button">open url</button>


</body>
</html>

6
  • So what is the problem? Commented Feb 5, 2015 at 10:22
  • If you want GET, then <a href="http://www.test.com/test.php?page=$page&user=$user" class="button">Open URL</a> with the appropriate CSS is the best way Commented Feb 5, 2015 at 10:24
  • I assume here's the mistake,<?php $page=$_POST['page']; $user=$_POST['user']; ?> Commented Feb 5, 2015 at 10:24
  • $POST_['page'] is wrong, use either $_POST['page'] for only post variable or $_REQUEST['page'] for both POST and GET variable Commented Feb 5, 2015 at 10:25
  • ( I assume here's the mistake,<?php $page=$_POST['page']; $user=$_POST['user']; ?> ) but what is the right one? i need to put data from $page in to java variable. Commented Feb 5, 2015 at 10:27

3 Answers 3

1

There is no need for scripting at all

If you want GET:

<?php
$page=$GET_['page']; // should be sanitized and you can use REQUEST for either 
$user=$GET_['user'];
$parm = "page=".$page."&user=".$user;
?>
<a href="http://www.test.com/test.php?<?php echo $parm; ?>" class="button">Open URL</a>

If you need to post:

<form action="test.php" method="post">
<input type="hidden" name="page" value="<?php echo $page; ?>"/>
<input type="hidden" name="user" value="<?php echo $user; ?>"/>
<input type="submit" value="Open URL" />
</form>
Sign up to request clarification or add additional context in comments.

6 Comments

good idea, but when i click it the url become like "test.com/test.php?<? echo $parm ?>"
Then you need <?php instead of just <?
Thank you mplungjan <?php was the idea, it is working now, thanks a lot man.
another question, in post methode i think there is some problem with value="<$php echo $page; ?> cause it puts <$php echo $page; ?> in the text box. how can i fix it?
Your page has to be rendered by the pho engine so it must end on .php and be served from a server. View source and the must be no <?php ... ?> anywhere
|
0

Change these lines:

<?php
$page=$POST_['page']; 
$user=$POST_['user']; 
<?

....

var user=<?php echo "$user";?>; 
var page=<?php echo "$page";?>; 
open('www.test.com/test.php?page='+page'&user='+user,'_self');

to this:

<?php
$page=$_POST['page']; //incorrect $_POST declaration
$user=$_POST['user']; //incorrect $_POST declaration
?> //php tag incorrectly closed

....

var user=<?php echo $user;?>; //echoing a variable not string (no need for quotes)
var page=<?php echo $page;?>; // echoing a variable not string (no need for quotes)
open('www.test.com/test.php?page='+page+'&user='+user,'_self'); // link was broken, forget to put '+' after page variable in link.

Comments

0
  1. You can create a form and via Javascript just run YOURFORMNAME.submit();
  2. Use href in javaScript to move to another location:

    location.href="www.test.com/test.php?page='+page'&user='+user"

1 Comment

thanks for your answer federico but i dont want to have form i can only have a button there in my page. i think the problem is data are not going into java var from php variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.