Jump to content

passing values to a new page when clicking on a link


kikilahooch

Recommended Posts

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???
Link to comment
Share on other sites

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

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??
Link to comment
Share on other sites

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:
[code]<?php if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>'; ?>[/code]

What do you see in both cases?

Ken
Link to comment
Share on other sites

[!--quoteo(post=363769:date=Apr 11 2006, 02:47 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 11 2006, 02:47 PM) [snapback]363769[/snapback][/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:
[code]<?php if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>'; ?>[/code]

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/~ciaracousins/admin_delete.php?login=true&id=4">
Link to comment
Share on other sites

[!--quoteo(post=363822:date=Apr 11 2006, 05:29 PM:name=kikilahooch)--][div class=\'quotetop\']QUOTE(kikilahooch @ Apr 11 2006, 05:29 PM) [snapback]363822[/snapback][/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/~ciaracousins/admin_delete.php?login=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:

[code]$query = "SELECT * FROM product where shopId = $shopId";


[/code]

if I change it to [code] $query = "SELECT * FROM product;[/code]

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
Link to comment
Share on other sites

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

Yeah tried that, still not working
Link to comment
Share on other sites

"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.
Link to comment
Share on other sites

[!--quoteo(post=363839:date=Apr 11 2006, 06:16 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Apr 11 2006, 06:16 PM) [snapback]363839[/snapback][/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
Link to comment
Share on other sites

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

[code]<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.'>
[/code]

but it is not dislaying the contents of shopName

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

I got this result:



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

\'.$shopName.\' [/code]

Anymore suggestions???
Link to comment
Share on other sites

[code]<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;?>"/>
[/code]

And then retrieve shopName from the POST array (since the form method is [b]post[/b])
[code]$shopName = $_POST['shopName'];[/code]
Link to comment
Share on other sites

[code]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']);
[/code]


It is still returning


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

\'.$shopName.\'
[/code]

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

[code]

....

    <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>
[/code]
Link to comment
Share on other sites

If your "<form>" tags are not within your "<?php ?>" tags you need to go into PHP to echo the value:
[code]
   <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>
[/code]


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
Link to comment
Share on other sites

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


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

on the top of my admin_update.php page I have this code:
[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']);[/code]

Link to comment
Share on other sites

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

At the start of your processing code, change
[code]<?php if(isset($_GET)) echo '<pre>' . print_r($_GET,true) . '</pre>';  ?>[/code]
to be
[code]<?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. :-(');
[/code]

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

Ken
Link to comment
Share on other sites

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

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

There is still a problem with $shopName. :-([/code]

this is being returned when i entered that code.

when i viewed the source code it showed:[code]

<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. :-([/code]
Link to comment
Share on other sites

oh right, heres the source before
[code]

    <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>[/code]


[code]
<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>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.