views:

38

answers:

3

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.

+1  A: 

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
hugo_leonardo
A: 

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.

kevtrout
i don't like this solution because just few people (at least that i know) uses or even have a desktop email software.
hugo_leonardo
@hugo: Here's a 2009 report on email client usage: http://www.campaignmonitor.com/stats/email-clients/ Desktop clients aren't quite dead yet, but it seems true that their usage could be declining in the long run. But keep in mind, if this is an intranet application, the usage of a specific client might be mandated by company policy. In that case, using the mailto attribute is reliable.
kevtrout
+2  A: 

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.

abelenky
I think OP meant something like `<script> var x = 42;</script> <?php echo $x ?>` outputting 42.
Marc B