I have a project that no one can or wants to engage in. The data is in a database table.
The code displays a menu, selects a target (url - http://.....), displays the record - so
far so good....then I want to click the button and redirect to the url.... not so good.
Can someone please advise.

<!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(); exit; }
else
{echo "Connected to mySQL</br>";}
//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select lookup account---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<p><p>
<CENTER>then

<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT id, target, username, password, purpose, emailused,lastused,
count FROM lookuptbl WHERE target = '".$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">'.'target'.'</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";
$target="target";
$count="count";
$lastused="lastused";
$sql = "UPDATE lookuptbl SET lastused=NOW(), count=$count + 1 WHERE target=$target";
if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }
else { echo "Error updating record: " . $con->error; }
}
?>
<p>
<button onclick="myFunction('<?php echo $target;?>')">go to url</button>
<?php
?>
</body></html> 

Quoted Text Here

Recommended Answers

All 5 Replies

What you mean by not so good? Is it not working?

So, on line 60, you are calling a javascript function called myfunction() that does not appear anywhere in your code:

Uncaught ReferenceError: myFunction is not defined
    onclick http://localhost/:1
localhost:1:1
    onclick http://localhost/:1

I was having a very hard time understanding your code, so I ran it through a PHP formatter to indent it properly:

<!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();
    exit;
}
else
{
    echo "Connected to mySQL</br>";
}
//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">' . '--- Select lookup account---' . '</option>';
$query = mysqli_query($con, "SELECT target FROM lookuptbl");
$query_display = mysqli_query($con, "SELECT * FROM lookuptbl");
while ($row = mysqli_fetch_array($query))
{
    echo "<option value='" . $row['target'] . "'>" . $row['target'] . '</option>';
}
echo '</select>';
?>
<p><p>
<CENTER>then

<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['target']))
{
    $name = $_POST['target'];
    $fetch = "SELECT id, target, username, password, purpose, emailused,lastused,
count FROM lookuptbl WHERE target = '" . $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">' . 'target' . '</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";
    $target = "target";
    $count = "count";
    $lastused = "lastused";
    $sql = "UPDATE lookuptbl SET lastused=NOW(), count=$count + 1 WHERE target=$target";
    if ($con->query($sql) === true)
    {
        echo "Record updated successfully";
    }
    else
    {
        echo "Error updating record: " . $con->error;
    }
}
?>
<p>
<button onclick="myFunction('<?php echo $target; ?>')">go to url</button>
<?php
?>
</body></html>

However, what are you wanting the button to do? Redirect to what? Is myFunction() defined in some external javascript file?

What are you expecting to be the value of $target? I see on line 51 you set the value of $target to the literal string 'target'. So you want to call myFunction('target')? Keep in mind that target is just a string with actual value of the six letters 'target' ... Nothing to do with $row['target'] whose value changes on each row of the database.

You could try making the url into a standard html link...
A plain url is plain text, so goes nowhere.

at line 60 u can use simply the anchor tag <a> instead of a button and JS like
<?php echo "<a href=" & $target & ">go to url </a>";?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.