ikasarie
ikasarie

Reputation: 27

How to get variable in url

I am having problem in getting variable from one PHP page to another PHP page. The problem is I can't get the variable. I use this code to move to another page :

$updateGoTo = "closeticketscs.php";
if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
}

and I've tried code below to get the variable :

$updateGoTo = "closeticketscs.php?id=echo $row_searchreslt['ID'];";
if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
}

However, this doesn't work. Any help will be appreciated. Here is the right code :

$updateGoTo = "closeticketscs.php?id=".$_POST['ID_Pelanggan'];
  if (isset($_SERVER['QUERY_STRING'])) 
  {
    $updateGoTo .= ((strpos($updateGoTo, '?') > -1) ? "&" : "?" ).
    $_SERVER['QUERY_STRING'];
  }

header(sprintf("Location: %s", $updateGoTo));

Upvotes: 0

Views: 3681

Answers (4)

Dwikky Maradhiza
Dwikky Maradhiza

Reputation: 56

in your second code, i think you have a mistake. you dont need to echo a php variable file when you declare a variable to another variable.

change

  $updateGoTo = "closeticketscs.php?id=echo $row_searchreslt['ID'];";

to

  $updateGoTo = "closeticketscs.php?id=".$row_searchreslt['ID'];

and if you want to get a variable from page to another page, just use $_GET. example,

in index.php

 <?php
 $updateGoTo = "closeticketscs.php?id=".$row_searchreslt['ID'];
 echo '<a href="'.$updateGoTo.'">Go Here</a>';
 ?>

and in closeticketscs.php

 <?php
 $new_variable = $_GET['id'];
 //now you get the variable 'id' from the url closeticketscs.php?id=value
 ?>

if use form :

 <form method="get" action="closeticketscs.php">
 <input type="text" name="id" value="123" >
 <input type="submit" value="Click Here" name="click">
 </form>

Upvotes: 0

Gabriel
Gabriel

Reputation: 2021

//If your link is closeticketscs.php?id=12345

if (isset($_REQUEST['id']){
   $id= $_REQUEST['id'];
   echo $id;
}

Among $_REQUEST, $_GET and $_POST which one is the fastest? this is more about request get and post.

Upvotes: 0

Djomla
Djomla

Reputation: 616

Did you try $_GET?

if (isset($_GET['id']) {
    $id = $_GET['id']
}

Upvotes: 3

Svetoslav
Svetoslav

Reputation: 4686

if (isset($_SERVER['QUERY_STRING'])) {
   $updateGoTo .= ( (strpos($updateGoTo, '?') > -1) ? "&" : "?" ) . $_SERVER['QUERY_STRING'];
}

If you want to check for question mark with strpos you must use > -1 :) And you can .= in 1 line no need to make it on 2

Upvotes: 0

Related Questions