Page 1 of 1

passing value in query string

Posted: Sun Mar 21, 2010 3:35 am
by mannyee
hi guys!!

I am having a tough time in automatically passing a variable from one page to another through query string.

For instance, in the following code when the case is modify (the following code is in the index.php and $catId is the value obtained when a user clicks modify link in the index.php), i would like to pass the $catId automatically to modify.php without the user knowing anything about what's goin on...
case 'modify' :
$catId=$_GET['catId'];
$content = 'modify.php';
$pageTitle = 'Shop Admin Control Panel - Modify Category';
break;
i perceived a wild idea(i'm a php newbie) as follows:
case 'modify' :
$catId=$_GET['catId'];
$content = 'modify.php?catId=$catId';
$pageTitle = 'Shop Admin Control Panel - Modify Category';
break;
but that din't work.....so pls help guys.....

Re: passing value in query string

Posted: Sun Mar 21, 2010 5:27 am
by JesseBSmith
Hello Mannyee,

Taking a look at your code, everything seems correct. However, you need to use double quotes in order to have a variable included in a string and not be taken literal. Or you can use the option I used below.

Code: Select all

 
case 'modify' :
      $catId=$_GET["catId"];
      $content = 'modify.php?catId='.$catId;    
      $pageTitle = 'Shop Admin Control Panel - Modify Category';
break;
 
Let me know if that doesn't work.

Re: passing value in query string

Posted: Sun Mar 21, 2010 6:34 am
by mannyee
Hi Jesse!

I tried your code but it doesn't seem to work.....not because your code is incorrect but that i misinterpreted my logic. So still I am not able to get the stuff working as desired.

Actually, "modify.php" is a page where users don't have to view(or do anything else).....its more of a template which contains a form and where certain procedures take place and for those procedures to take place.....$_GET['catId'] is required......

this page is then assigned to $content in the case and later when the switch case exits.....
i use the function

Code: Select all

 
//after exiting the switch call the templating file
require_once '../include/template.php';

the snippet of the template.php looks as follows

Code: Select all

 
//the template.php is a templating file which i use for all other pages add.php,delete.php etc
 
<td> width="600" valign="top" class="contentArea"><table width="100%" border="0" cellspacing="0" cellpadding="20">
        <tr>
          <td>
<?php
require_once $content;   
?>
          </td>
        </tr>





what i want is not to redirect a user to the modify.php page (as i mentioned in the subject....pardon me).....only just to pass the $catId variable using get method, handle some stuffs there and then assign the page to the $content back in the index.php....and all this without any user involvement.

That means when the user click the modify link in the index.php all the work should be done and the user should see only the template.php that displays contents of the modify.php


If you still don't understand yet can i paste few code snippets so that you can understand the abstract?

Re: passing value in query string

Posted: Sun Mar 21, 2010 6:59 am
by michaeru
So your code is something like this?

-index.php

Code: Select all

 
<?php
 
  $catId=$_GET['catId'];
  $content = 'modify.php';
  $pageTitle = 'Shop Admin Control Panel - Modify Category';
 
  require_once('template.php');
 
?>
 
-template.php

Code: Select all

 
<?php
 
  // html codes
 
  <div id="contents">
 
    <?php
 
      require_once($content);
 
    ?>
 
  </div>
 
  // html codes
 
?>
 
If it is like that, you do not need to pass the value of $_GET['catID'] to the modify.php.
In your modify.php, just "get" the value of $_GET['catID'], because the index.php required/included the template which also required/included the modify.php. All non-private variables can be used/called by files that are required/included.

Something like this.

-modify.php

Code: Select all

 
<?php
 
  if(isset($_GET['catID'])) {
 
    // codes here
 
  }
 
?>
 
Well, requiring or including files is like concatenating a file with a file, hope you got it.

But then again, I'm also new to PHP.