How to code onclick

Hi, The below code produces a menu, I select an account to confirm username, etc. and want to go
to the selected “website” (a database field - http:// whatever.com). I’ve tried and tried but I
can’t figure out the onclick or whatever to go to the website.

<!DOCTYPE><html><head><title>lookup menu</title></head><body><center>
<form name="form" method="post" action="">
<?php
$con=mysqli_connect("localhost","root","","homedb");
//============== check connection
if(mysqli_errno($con))
{echo "Can't Connect to mySQL:".mysqli_connect_error();}
else
{echo "Connected to mySQL</br>";}
//This creates the drop down box
echo "<select name= 'website'>";
echo '<option value="">'.'--- Select lookup account---'.'</option>';
$query = mysqli_query($con,"SELECT website FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['website']."'>".$row['website']
.'</option>';}
echo '</select>';
?>
<p><p>
<CENTER>then

<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['website']))
{
$name = $_POST['website'];
$fetch="SELECT id,website, username, password, purpose, emailused,lastused,
count FROM lookuptbl WHERE website = '".$name."'";
$result = mysqli_query($con,$fetch);
if(!$result)
{echo "Error:".(mysqli_error($con));}
//display the table
echo '<table border="1">'.'<tr>'.'<td bgcolor="#ccffff align="center">'. 'lookup menu'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'
<td bgcolor="#CFB53B align="center">'.'id'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'website'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'username'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'password'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'purpose'. '</td>'.'
<td bgcolor="#ccffff align="center">'.'emailused'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'lastused'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'count'. '</td>'.'</tr>';
while($data=mysqli_fetch_row($result))
{echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td>
<td>$data[4]</td><td>$data[5]</td><td>$data[6]</td><td>$data[7]</td></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
$id="id";
$website="website";
$count="count";
$lastused="lastused";
$sql = "UPDATE lookuptbl SET lastused=NOW(), count=$count + 1 WHERE website=$website";
if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }
else { echo "Error updating record: " . $con->error; }
}
?>
<p>
<button onclick="myFunction()">go to website</button>
<script>
function myFunction() {
  document.getElementById("website").innerHTML = "";
}
</script>
<?php
?>
</body></html>

I have no experience with the above code, but you could basically reproduce that behaviour using an anchor tag inside the button.
Asuming you wanna make it using the onclick event try putting this in your function:
window.open('https://example.com', '_blank')

try putting some console logs to see whether the code is executing.
Also suggest you put the code on a codepen or somewhere like that so people can try it and play with it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.