Jump to content

Help integrating PHP, MySQL and javascript


lukep11a

Recommended Posts

Hi, hope I have posted this in the right place, I have the following code which contains PHP and javascript, where both work on there own but when I try putting them together I keep getting errors:

 

<?php
include 'config.php';
include 'opendb.php';

$var = 'Smith';
$query = "SELECT * FROM search_directory WHERE merchant LIKE \"%$var%\"";
$result = mysql_query($query);
$id = $row{"id"};

while($row = mysql_fetch_assoc($result))
{
    echo "Name: {$row['name']} <br>";
    echo "Subject: {$row['title']} <br><br>";	
}
include 'closedb.php';
?>

<a href="javascript:;" onclick="document.getElementById('.....').style.display='block';window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a>
<div id="....." style="display:none;">
...your text here
</div>

 

What I have been trying to do is get $id variable from the php code to replace the '.....' in the javascript and div id. I have tried putting it into an echo statement which seemed to work with the div id but the brought up errors when i tried with the javascript. If anyone knows how to solve this it would be very much appreciated. Thanks in advance.

Link to comment
Share on other sites

You should be able to replace the ..... with <?php echo $id; ?>

 

Does that not work?

 

Also, on line 8, which says "$id = $row{"id"};", that should look like this:

$id = $row["id"];

 

You should use square braces when notating an array.

 

Lastly, you are trying to store the value of $row['id'] into the variable $id, but when you are doing that assignment the variable $row is not defined yet.

Link to comment
Share on other sites

Thanks, thats brilliant, managed to get it to work using what you said! Do you know how to make this part of the coding into a repeating region?

 

<a href="javascript:;" onclick="document.getElementById('<?php echo $id; ?>').style.display='block';window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a>
<div id="<?php echo $id; ?>" style="display:none;">
...your text here
</div>

Link to comment
Share on other sites

Lines 12 and 13 are the repeating lines. If you insert that code in between the curly brackets on line 11 and 14, those will repeat. It will look something like this:

 


while($row = mysql_fetch_assoc($result))
{
    echo "Name: {$row['name']} <br>";
    echo "Subject: {$row['title']} <br><br>";
    
    ?>
    <a href="javascript:;" onclick="document.getElementById('<?php echo $row['id']; ?>').style.display='block'; window.open('http://www.google.co.uk');"><img src="../../graphics/Click here.png" border="0" /></a>
    <div id="<?php echo $row['id']; ?>" style="display:none;">
        ...your text here
    </div>
    <?php
}

Link to comment
Share on other sites

Thanks for your quick response, that has made the image appear twice for the two rows it represents in the table which is correct but now when you click on the image to go to the link nothing happens and no hidden text is displayed... any ideas?

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.