Jump to content


Photo

passing values to a new page when clicking on a link


  • Please log in to reply
18 replies to this topic

#1 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 11 April 2006 - 06:51 PM

I'm having problems passing values from one page to another. I need to pass a user id from one page to another when a link is clicked on. I did this once with a log in page, when the user clicked on the submit button is directed the user to the next page and passed in the userId with it. I used this line of code for that:

header("Location: index2.php?login=true&id=$userName");

But this time I want to be able to pass the same values from this new page onto another new page, but it is not a button being used this time, but a link. Any help on how i go about this please???

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 06:55 PM

Just append the values onto the URL:
<?php echo '<a href="anotherscript.php?login=true&amp;id=' . $userName . '">This is a link</a>'; ?>

Ken

#3 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 11 April 2006 - 07:23 PM

[!--quoteo(post=363757:date=Apr 11 2006, 01:55 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 11 2006, 01:55 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Just append the values onto the URL:
<?php echo '<a href="anotherscript.php?login=true&amp;id=' . $userName . '">This is a link</a>'; ?>

Ken
[/quote]

Thanks Ken, that is now passing variables through for me but the contents of the variable that was passed through is not being displayed. Here is the code that i used:

************************
$Id= $_GET['id']; //id was passed through from my login page. If i do an echo stmt here it displays the content of $Id

<?php echo '<a class="orange" href ="http://snet.wit.ie/~ciaracousins/admin_delete.php?login=true&id='.$Id.'">Click here to modify and delete Records</a>'; ?>


****************
$shopId =$_GET['id'];

echo "$shopId"; //only displays $Id



Any suggestions where I am going wrong??

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 April 2006 - 07:47 PM

I'm not sure. When I plug your code into a short test script, it's fine.

After the page is displayed that contains the link, do a "Show source" and see what the link looks like.

In your script delete_admin.php put the following line at the start of the script:
<?php if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>'; ?>

What do you see in both cases?

Ken

#5 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 11 April 2006 - 10:29 PM

[!--quoteo(post=363769:date=Apr 11 2006, 02:47 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 11 2006, 02:47 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I'm not sure. When I plug your code into a short test script, it's fine.

After the page is displayed that contains the link, do a "Show source" and see what the link looks like.

In your script delete_admin.php put the following line at the start of the script:
<?php if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>'; ?>

What do you see in both cases?

Ken
[/quote]

when i put that code in I get this:

Array
(
[login] => true
[id] => 4
)

4


and when i look at the source i shows up as:

<a class="orange" href ="http://snet.wit.ie/~...gin=true&id=4">

#6 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 11 April 2006 - 10:48 PM

[!--quoteo(post=363822:date=Apr 11 2006, 05:29 PM:name=kikilahooch)--][div class=\'quotetop\']QUOTE(kikilahooch @ Apr 11 2006, 05:29 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
when i put that code in I get this:

Array
(
[login] => true
[id] => 4
)

4
and when i look at the source i shows up as:

<a class="orange" href ="http://snet.wit.ie/~...gin=true&id=4">
[/quote]


looking at it now its seems to be working, thanks Ken :)

what i was actually trying to so was pass in the value of shopId so that i could update and delete items in my product table whos shopId was the one i passed in. I am trying to use this statement but it is returning no values:

$query = "SELECT * FROM product where shopId = $shopId"; 




if I change it to
$query = "SELECT * FROM product;

it returns all items in the table product. Is there a problem with the syntex?? I am very new to all of this! Thanks for your help
Ciara

#7 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 April 2006 - 10:56 PM

Try this, then:
$query = "SELECT * FROM product where shopId = '$shopId' ";  

Legend has it that reading the manual never killed anyone.
My site

#8 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 11 April 2006 - 11:09 PM

[!--quoteo(post=363829:date=Apr 11 2006, 05:56 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 11 2006, 05:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try this, then:
$query = "SELECT * FROM product where shopId = '$shopId' ";  
[/quote]

Yeah tried that, still not working

#9 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 April 2006 - 11:16 PM

"Not working" covers a multitude of sins. Do you know absolutely that there are matching data in that table? If so, then add proper error trapping to the query statement to shed some light on what's really happening ... and maybe show us more than one line of the 'not working' code as well.
Legend has it that reading the manual never killed anyone.
My site

#10 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 11 April 2006 - 11:54 PM

[!--quoteo(post=363839:date=Apr 11 2006, 06:16 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 11 2006, 06:16 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
"Not working" covers a multitude of sins. Do you know absolutely that there are matching data in that table? If so, then add proper error trapping to the query statement to shed some light on what's really happening ... and maybe show us more than one line of the 'not working' code as well.
[/quote]


Thanks for the advice Andy, it was a problem with table names not the code

#11 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 April 2006 - 01:05 PM

I'm having similar problems again today tryin to pass values through. This time I have a product table which i want to be able to update. I enter a product id into a txt field and it brings up the values of that particular product where i can edit them. What i want to do is to only allow the user to update products that are belonging to them. I am passing through the name of the shop to identify the user. I'm not sure how i pass it through in this scenario. I have tried appending it to the form action

<form action="admin_update.php?login=true&shopName='.$shopName.'" method="post"

...
<td><input type="text" name="prodId" size="10" maxlength="5"/>
 <input type="hidden" name="shopName" value=value='.$shopName.'>

but it is not dislaying the contents of shopName

I put this piece of code into my admin_update.php file
$shopName= $_GET['shopName'];
if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>';    
echo "$shopName";

I got this result:



and got this result:
Array
(
    [login] => true
    [shopName] => \'.$shopName.\'
)

\'.$shopName.\'

Anymore suggestions???

#12 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 12 April 2006 - 01:18 PM

<form action="admin_update.php?login=true" method="post">
...
<td><input type="text" name="prodId" size="10" maxlength="5"/>
<input type="hidden" name="shopName" value="<?php echo $shopName;?>"/>

And then retrieve shopName from the POST array (since the form method is post)
$shopName = $_POST['shopName'];

Legend has it that reading the manual never killed anyone.
My site

#13 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 April 2006 - 02:26 PM

I tried putting that in but it didn't work, I already had this in anyway

[code]f(isset($_POST['submittedUpdate'])){
        
         $prodId= trim($_POST['prodId']);
         $shopName = trim($_POST['shopName']);


It is still returning


Array
(
    [login] => true
    [shopName] => \'.$shopName.\'
)

\'.$shopName.\' 

Can you see where I am going wrong??

Here is the code from my delete_admin.php page. I am trying to send to info from this page to my admin_update.php page


....

     <form action="admin_update.php?login=true&shopName=$shopName" method="post">

<center>
<table>
<tr>
      <td><font color="black"><b>Enter Product ID to UPDATE:</b></font>

      

    <td><input type="text" name="prodId" size="10" maxlength="5"/>
        <input type="hidden" name="shopName" value="$shopName">

    </table>
</center>
<center><input type="submit" name="submit" value="SUBMIT"/>
  </p>
</center>
<input type="hidden" name="submitted" value="TRUE"/>
</form>
</td>
<td>


<form action="admin_delete.php" method="post">

<center>
<table>
<tr>
  <td><font color="black"><b>Enter Product ID to DELETE:</b></font>
  

    <td><input type="text" name="prodId" size="10" maxlength="5"/>
    <td><input type="hidden" name="shopName" value='.$shopName.'/>
  
</table>
</center>

<center><input type="submit" name="submit" value="SUBMIT"/>
  </p>
</center>
<input type="hidden" name="submitted" value="TRUE"/>
</form></td></tr></table></center>

<?php #Script 7.3 - register.php

//if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>';
    
    include "db.php";
    
    $shopId =$_GET['id'];

    $sql = "select shopName from shops where shopId = '$shopId';";
    
    //db
    $result = mysql_query($sql,$conn) or die(mysql_error());

    //get the number of rows in the result set; should be 1 if a match
    if (mysql_num_rows($result) >= 1) {
    //if authorized, get the values of shop name
    $shopName = mysql_result($result, 0, 'shopName');
    

    }

    if(isset($_POST['submitted'])){

        $prodId= trim($_POST['prodId']);
        $shopName= trim($_POST['shopName']);
        $shopName = $_POST['shopName'];

        
        $result = @mysql_query($query2);
            if($result){
                echo'<p align=center><font color="White"><b>DELETED SUCCESSFULLY</b></font></p>';
            }
    
            else{
                echo'<h1> System Error </h1>';
            }

    }

    
        
    
      $query = "SELECT * FROM product where shopName = '$shopName' ";  

    
    $result = @mysql_query($query);
    if($result){
        echo'
                
            <h1><font color=#FFFFFF> Product Details</font></h1>

            
            <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="Blue">
                <tr>
        
                <td align="left" bgcolor="Blue"><center><font color="#FFFFFF"><b>Product Id</b></center></td>
                <td align="left" bgcolor="Blue"><center><font color="#FFFFFF"><b>Shop Name</b></td>
                <td align="left" bgcolor="Blue"><center><font color="#FFFFFF"><b>Product Name</b></td>
                            
                

    
                </tr>';
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                    echo'<tr>
<td align="center">'.$row['prodId'].'</td>
<td align="center">'.$row['shopName'].'</td>
<td align="center">'.$row['prodName'].'</td>
                

                    </tr>';
                        }
        echo'</table>';
    }
    
    else{
        echo'<h1> System Error </h1> table ';
        exit();
    }
    mysql_close();

?>

</body>
</html>


#14 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 02:34 PM

If your "<form>" tags are not within your "<?php ?>" tags you need to go into PHP to echo the value:
   <form action="admin_update.php?login=true&shopName=<?php echo $shopName ?>" method="post">

<center>
<table>
<tr>
      <td><font color="black"><b>Enter Product ID to UPDATE:</b></font>

      

    <td><input type="text" name="prodId" size="10" maxlength="5"/>
        <input type="hidden" name="shopName" value="<?php echo $shopName ?>">

    </table>
</center>
<center><input type="submit" name="submit" value="SUBMIT"/>
  </p>
</center>
<input type="hidden" name="submitted" value="TRUE"/>
</form>
</td>
<td>


<form action="admin_delete.php" method="post">

<center>
<table>
<tr>
  <td><font color="black"><b>Enter Product ID to DELETE:</b></font>
  

    <td><input type="text" name="prodId" size="10" maxlength="5"/>
    <td><input type="hidden" name="shopName" value="<?php echo $shopName ?>"/>
  
</table>
</center>

<center><input type="submit" name="submit" value="SUBMIT"/>
  </p>
</center>
<input type="hidden" name="submitted" value="TRUE"/>
</form></td></tr></table></center>


BTW, you don't need the hidden names like "submitted". To check whether a form has been submitted just check if $_POST['submit'] is set.

Ken

#15 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 April 2006 - 02:48 PM

I tried that and now it gives me back the following:


Array
(
    [login] => true
    [shopName] => 
)

on the top of my admin_update.php page I have this code:

<?php
    include "db.php";


$shopName= $_GET['shopName'];
$shopName = $_POST['shopName'];
if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>';    
    if(isset($_POST['submittedUpdate'])){
        
$prodId= trim($_POST['prodId']);
        $shopName = trim($_POST['shopName']);



#16 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 03:08 PM

Which of these do you want?
<?php
$shopName= $_GET['shopName'];
$shopName = $_POST['shopName'];
?>

At the start of your processing code, change
<?php if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>';  ?>
to be
<?php
if(isset($_GET)) echo '<pre> $_GET: ' . print_r($_GET,true) . '</pre>'; 
if(isset($_POST)) echo '<pre> $_POST: ' . print_r($_POST,true) . '</pre>';
$shopName = (isset($_GET['shopName']))?trim($_GET['shopName']):'';
$shopName = (isset($_POST['shopName']) && $shopName == '')?trim($_POST['shopName']):'';
if ($shopName == '') exit('There is still a problem with $shopName. :-(');

Also before submitting the form, do a show source to see what in contained in the form.

Ken

#17 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 April 2006 - 03:31 PM

$_GET: Array
(
    [login] => true
    [shopName] => 
)

 $_POST: Array
(
    [prodId] => 38
    [shopName] => 
    [submit] => SUBMIT
    [submitted] => TRUE
)

There is still a problem with $shopName. :-(

this is being returned when i entered that code.

when i viewed the source code it showed:

<pre> $_GET: Array
(
    [login] => true
    [shopName] => 
)
</pre><pre> $_POST: Array
(
    [prodId] => 38
    [shopName] => 
    [submit] => SUBMIT
    [submitted] => TRUE
)
</pre>There is still a problem with $shopName. :-(


#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 April 2006 - 03:48 PM

No, view the source code before you submit the form. We need to see what the hidden values are and the form action line.

Ken

#19 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 12 April 2006 - 04:00 PM

oh right, heres the source before

     <form action="admin_update.php?login=true&shopName="  method="post">

<!--///////////END CHANGE FILE NAME//////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////-->

<center>
<table>
<tr>
    <!--////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////(A2) -CHANGE HEADING HERE TO <tableNAME> ID TO UPDATE///////////////////-->

  <td><font color="black"><b>Enter Product ID to UPDATE:</b></font>

    <!--///////////END CHANGE HEADING ////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////////-->

  

    <!--////////////////////////////////////////////////////////////////////////
    ////////(A3) - CHANGE 'NAME' TO PRIMARY KEY FOR APPROPRIATE TABLE/////////-->

    <td><input type="text" name="prodId" size="10" maxlength="5"/>
        <input type="hidden" name="shopName" value="">

    <!--//////////END CHANGE 'NAME'/////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////-->
  
</table>
</center>
<center><input type="submit" name="submit" value="SUBMIT"/>
  </p>
</center>
<input type="hidden" name="submitted" value="TRUE"/>
</form>


<form action="admin_delete.php" method="post">


<center>
<table>
<tr>
  <td><font color="black"><b>Enter Product ID to DELETE:</b></font>
  

    <td><input type="text" name="prodId" size="10" maxlength="5"/>
    <td><input type="hidden" name="shopName" value="">


</table>
</center>

<center><input type="submit" name="submit" value="SUBMIT"/>
  </p>
</center>
<input type="hidden" name="submitted" value="TRUE"/>
</form></td></tr></table></center>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users