0

I have a list of values that upon hyperlink 'onclick' will display the associated recordset in an array below.

Through PHP, I have the dataset of connected table values working. Now I need to link that dataset to the onclick event.

Through my research there are a few ways I can go about this: iFrames (seems not recommended); JSON/AJAX (languages which I do not know and none of my other data uses this construct, but is it possible to use straight PHP?);

$_GET variable with a related function seems to be the way to go though am struggling to get the right syntax to create a response. Will this be using a filter function?

essentially: $row_artistrecordset['artist'] = $row_getartists['artist']. If I can base the code on this match construct it will hopefully be versatile enough for use in other pages.

This is my code so far:

<?php
mysql_select_db($database_connectmysql, $connectmysql);
$query_artistrecordset = "SELECT * FROM artists ORDER BY artist ASC";
$artistrecordset = mysql_query($query_artistrecordset, $connectmysql) or die(mysql_error());
$row_artistrecordset = mysql_fetch_assoc($artistrecordset);
$totalRows_artistrecordset = mysql_num_rows($artistrecordset);
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0   Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>artistindex</title>
  <link href="exhibitstyles/exhibitstyles.css" rel="stylesheet" type="text/css" /> 
  </head>
  <body>    
 <?php include_once("exhibitstyles/header.php");?> 
 <div id="singlemid" class="paragraph" style="text-align:center"> 
<div id="artistlist">
<?php do { ?>
<a href=javascript:function_$_GET(artistindex.php?[$row_artistrecordset['artist']]);>; title="artistlink" class="list"><?php echo $row_artistrecordset['artist']; ?></a>&nbsp;|
<?php } while ($row_artistrecordset = mysql_fetch_assoc($artistrecordset)); ?>
</div>
<br />    
<div id="searchresults">

function_$_GET(artistindex.php?[$row_artistrecordset['artist']]) {
$.post("artistindex.php",'v=.$row_getartists['artist'].' + value, function $_GET(artistindex.php?[$row_artistrecordset['artist']]) {
      $query_getartists.html().trigger("create");
});
}

<?php
mysql_select_db($database_connectmysql, $connectmysql);
 echo <a href=javascript:function_$_GET(artistindex.php?[$row_artistrecordset['artist']]);>.$row_getartists['artist'].;
 $query_getartists = "SELECT * FROM artists WHERE artist = '".$row_artistrecordset['artist']."' ORDER BY artist ASC";
 $getartists = mysql_query($query_getartists, $connectmysql) or die(mysql_error());
      while ($row_getartists = mysql_fetch_assoc($getartists)) {              
                echo $row_getartists['artist'], "<br>";
                echo $row_getartists['website'], "<br>";
                echo $row_getartists['artist_statement'], "<br>";
                echo $row_getartists['image'], "<br>";
 $artistlink = $row_getartists['artist'];
 $query_getseries = "SELECT * FROM series WHERE artist='$artistlink' ORDER BY exhibition ASC";
 $getseries = mysql_query($query_getseries, $connectmysql) or die(mysql_error());
      while ($row_getseries = mysql_fetch_assoc($getseries)) {
                echo $row_getseries['series'], "<br>";
                echo $row_getseries['exhibition'], "<br>";
                echo $row_getseries['series_statement'], "<br>";
                echo $row_getseries['image'], "<br>";
 $serieslink = $row_getseries['series'];
 $query_getpieces = "SELECT * FROM pieces WHERE series='$serieslink'";
 $getpieces = mysql_query($query_getpieces, $connectmysql) or die(mysql_error());
      while ($row_getpieces = mysql_fetch_assoc($getpieces)) {
                echo $row_getpieces['piece'], "<br>";
                echo $row_getpieces['category'], "<br>";
                echo $row_getpieces['dimensions'], "<br>";
                echo $row_getpieces['price'], "<br>";
                echo $row_getpieces['description'], "<br>";
                echo $row_getpieces['image'], "<br>";
      }
}
?>
</div>
</div>
</body>
</html>
<?php
mysql_free_result($getartists);

mysql_free_result($getseries);

mysql_free_result($getpieces);
      }
?>
17
  • If I am doing this entirely wrong, please tell me. Other questions I have found trying to achieve a similar goal have not been answered either so it is hard to know what is wrong with this logic. Commented Jul 22, 2013 at 0:18
  • "onclick" defines a Javascript function that will execute when the element is clicked. To do something such as what you want will require either changing to another PHP script which will generate the new page or much nicer an AJAX call which will refresh the data on the page without reloading it. Check out jQuery for a truly easy way to use AJAX. Commented Jul 22, 2013 at 0:37
  • Right! So I am doing this wrong and using js unwittingly...thankyou! that is what I needed to know! Commented Jul 22, 2013 at 0:51
  • @mlewis54 o dear this is now even deeper as I don't quite understand the jquery language. not sure where to begin so would be able to explain and give pointers as to where and how? For example, is my while loop already js language within a php block? Commented Jul 22, 2013 at 0:55
  • @sarah369 I'm curious. Is your <a href="#"onclick= working? I've tried that method and it didn't work for me. I use something along the line of <a href="javascript:function();">Link</a>. Commented Jul 22, 2013 at 1:02

1 Answer 1

0

You want an action to take place when the user clicks something. Let's say you have a table of values and you want to get further information about a value. The html might look like this:

<table width=100% cellpadding=0 cellspacing=0>
<tr>
<td width=30% valign=top><span onclick="alert('Value 1 was clicked');">1</span></td>
<td width=70% valign=top>Description of Value 1</td>
</tr>
<tr>
<td width=30% valign=top><span onclick="alert('Value 2 was clicked');">1</span></td>
<td width=70% valign=top>Description of Value 2</td>
</tr>
</table>

What will happen here is that the javascript alert() function will be called when you click on either of the two leftmost columns of the table.

You could also pass the information to your own script as in:

    <script>
    function myOwnAlert(value) {
         alert(value);
    }
    </script>
<div id="myTable">
    <table width=100% cellpadding=0 cellspacing=0>
    <tr>
    <td width=30% valign=top><span onclick="myOwnAlert('Value 1 was clicked');">1</span></td>
    <td width=70% valign=top>Description of Value 1</td>
    </tr>
    <tr>
    <td width=30% valign=top><span onclick="myOwnAlert('Value 2 was clicked');">1</span></td>
    <td width=70% valign=top>Description of Value 2</td>
    </tr>
    </table>
</div>

The net effect is the same, a message was displayed. However, you could also update the table dynamically using jQuery. There are a number of tutorials on jQuery available online. You will basically wind up with a javascript function such as:

<script>
function myOwnAlert(value) {
    $.post("getnewtable.php",'v=' + value, function(data) {
          $("#myTable").html().trigger("create");
    });
}
</script>

This will result in the contents of your table being replaced with the output of the your getnewtable.php script.

Note: This is not meant to be a prime example of using either HTML or jQuery -- just a very mini-tutorial.

Sign up to request clarification or add additional context in comments.

3 Comments

ok, so focusing on the last example you gave do I place this as the href"" and is this correct: function_$_GET(artistindex.php?[$row_artistrecordset['artist']]) { $.post("artistindex.php",'v=.$row_getartists['artist'].' + value, function $_GET(artistindex.php?[$row_artistrecordset['artist']]) { $query_getartists.html().trigger("create"); });
I thought that such a 'if' 'else' construct was php? I am confused as to the line between what is php and what is java...
If you're going to use javascript I would not use an href, that's why I surrounded the values with <span></nospan> tags. it would be more like: function getArtistIndex(v) { then the $.post call) } The onclick call triggers your javascript function which then makes a call to a php script. If/else constructs appear in many languages. The code between <script> and </script> tags is javascript (not java). There is no PHP in my example, just javascript and HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.