开发者

click a link pass a session variable to the next page

开发者 https://www.devze.com 2023-01-14 06:40 出处:网络
I have a page where I am using a PHP while loop to echo the names and addresses of our dealers.For the email link, I want the user to be able to click the link, which would then take him to a form tha

I have a page where I am using a PHP while loop to echo the names and addresses of our dealers. For the email link, I want the user to be able to click the link, which would then take him to a form that emails that person.

I know that you can't mix Javascript and PHP, so how do I tell which link the user has clicked on so that I can pass that variable to the form on the next page? Here is my code:

<?php while($row = mysql_fetch_array($result)) { 
          if ($row['active'] == 1) {

?>

      <div id="dealer">

          <div id="dealername">
              <h3 style="float:left;"><?php echo $row['company']; ?></h3><br/>
              <p><?php echo $row['address1']; ?><br/>
                 <?php if($row['address2'] || $row['address3']) { 
                        echo $row['address2'] . ' ' . $row['address3'] . '<br/>'; } ?>
                 <?php echo $row['city']; 
                 if ($row['state']) {
                          echo ', ' . $row['state'];
                  }
                  echo '&nbsp;&nbsp;' . $row['zip']; ?>
                  <br />
                  <?php echo $row['country']; ?>

                  <br />
                  <br />
                  Phone: <strong><?php echo $row['phone1']; ?></strong><?php if ($row['phone2']) {
                      echo ' or ' . $row['phone2']; } ?><br/>
                  <?php if($row['fax']) { echo 'Fax: ' . $row['fax'];  } ?><br />
              </p>
          </div>

          <?php if ($row['email']) {
              echo '<div class="dealerEmail">'. // need help here . '</div>';

            } ?>

        <br class="clearall" />
        </div>




<?php }} ?>

The page that this code appears on is pulling data from a database using a GET statement based on a URL similar to "index.php?state=az".

I've tried making the email link use a "?email=..." format but when I go to the dealer-email.php page there are no parameters in the URL, which I don't get. That's why I'm trying to find some other way 开发者_如何转开发to pass some kind of variable to the next page, based on which link the user clicks on.

Hope this is clear. Thanks in advance for your help.


this should work fine:

<a href="somepage.php?email=<?php echo $row['email']; ?>">contact dealer</a>

and then you retrieve the data in the somepage.php page:

if(isset($_GET['email']))
    $email = $_GET['email'];
else
    //redirect somewherelse


I know that you can't mix Javascript and PHP

This is not true. Javascript is a Client-Side Scripting language, and PHP is Server-Side scripting. They run at different times, on different machines, and will run just fine.

Mixing Javascript and PHP might be a mental challenge and subject to pitfalls, but it definitely can be done, and often is done.


You might skip the email form altogether and include a mailto attribute in your link.

<a href="mailto:<?php echo $row['email'];?>">contact dealer</a>

This will open the user's default email client new message window with the address filled in. I understand you may want to use a custom form to prevent someone from spamming all your contacts, but if this is a restricted area for logged in users, or an intranet site, this might work.

0

精彩评论

暂无评论...
验证码 换一张
取 消