1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to use on-click

Discussion in 'PHP' started by pshaw, Sep 11, 2022.

  1. #1
    Hi, a little help, please. I have been using:

    onclick="gotolink()
    in an html program to keep show passwords, click on the button and go to the url.

    This BELOW code selects a record from a database table and updates the date visited and times visited.
    I want to use the onclick here to go to the url also.

    the code:
    <!DOCTYPE><html><head><title>email 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= 'target'>";
    echo '<option value="">'.'--- Select email 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></body></html>
    
    <!DOCTYPE><html><head></head>
    <body><center>
    <?php
    $con=mysqli_connect("localhost","root","","homedb");
    if(mysqli_errno($con))
    {echo "Can't Connect to mySQL:".mysqli_connect_error();}
    if(isset($_POST['target']))
    {
    $name = $_POST['target'];
    $fetch="SELECT id, target, username, password, purpose, emailused,lastused,
    visitcount, saved 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">'. 'Email 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">'.'visitcount'. '</td>'.'
    <td bgcolor="#CFB53B align="center">'.'saved' .'</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><td>$data[8]</td>
    </tr>");}
    echo '</table>'.'</td>'.'</tr>'.'</table>';
    
    $target='target';
    $visitcount='visitcount';
    $lastused='lastused';
    $saved='saved';
      
      $sql = "UPDATE lookuptbl SET visitcount=$visitcount + 1, lastused=NOW() ,
    saved='$saved'
    WHERE target='$target'";
      if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }
      else { echo "Error updating record: " . $con->error; }
      $con->close();
    
    }
    ?>
    </body></html>
    PHP:
    Thanks in advance for response!
     
    Last edited by a moderator: Sep 11, 2022
    pshaw, Sep 11, 2022 IP
  2. seomanualsubmission

    seomanualsubmission Well-Known Member

    Messages:
    905
    Likes Received:
    128
    Best Answers:
    4
    Trophy Points:
    165
    #2
    Little confusing. You want to use java code for open url on on click or something else.

    To open URL onclick, you can use below code.
    <script language="javascript">
    var sid;
    function myFunction(sid) {
      window.open(sid);
    }
    </script>
    Code (markup):
    And add below code where you want to use onclick function on website body .... Fetch website URL from database and add in My Function. Site will open without any issue.
    <button onclick="myFunction('<?php echo $row21["url"];?>')">Open Site</button>
    Code (markup):
     
    seomanualsubmission, Sep 11, 2022 IP
  3. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    is this what you mean:
    <!DOCTYPE><html><head><title>email 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= 'target'>";
    echo '<option value="">'.'--- Select email 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></body></html>
    
    <!DOCTYPE><html><head></head>
    <body><center>
    
    <script language="javascript">
    var sid;
    function myFunction(sid) {
      window.open(sid);
    }
    </script>
    
    <?php
    $con=mysqli_connect("localhost","root","","homedb");
    if(mysqli_errno($con))
    {echo "Can't Connect to mySQL:".mysqli_connect_error();}
    if(isset($_POST['target']))
    {
    $name = $_POST['target'];
    $fetch="SELECT id, target, username, password, purpose, emailused,lastused,
    count, saved 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>'.'
    <td bgcolor="#CFB53B align="center">'.'saved' .'</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><td>$data[8]</td>
    </tr>");}
    echo '</table>'.'</td>'.'</tr>'.'</table>';
    
    $target='target';
    $count='count';
    $lastused='lastused';
    $saved='saved';
      
      $sql = "UPDATE lookuptbl SET count=$count + 1, lastused=NOW() ,
    saved='$saved'
    WHERE target='$target'";
      if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }
      else { echo "Error updating record: " . $con->error; }
      $con->close();
    
    }
    ?>
    <button onclick="myFunction('<?php echo $row21["url"];?>')">Open Site</button>
    </body></html>
    PHP:
     
    Last edited by a moderator: Sep 11, 2022
    pshaw, Sep 11, 2022 IP
  4. seomanualsubmission

    seomanualsubmission Well-Known Member

    Messages:
    905
    Likes Received:
    128
    Best Answers:
    4
    Trophy Points:
    165
    #4
    Your code is looking fine .... After select when submit then all data you can see of related account.
    Actually i am not getting that where you want to use onclick function.
    Describe in a bit details so i or any other expert can help you here.
     
    seomanualsubmission, Sep 11, 2022 IP
  5. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    my objective is to select an account, assure I know the log in info and then go to that url.
    Before I close I want to update the date(and time)and visit count.
     
    pshaw, Sep 11, 2022 IP
  6. CarolWWW

    CarolWWW Greenhorn

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #6
    <element onclick="myScript">
     
    CarolWWW, Sep 12, 2022 IP
  7. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Carol, I'm sorry, what, where, why - I'm already lost! Can you be a little more succinct?
     
    pshaw, Sep 12, 2022 IP
  8. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    this is my best progress:
    screen 1) I click on menu and see all options,]
    screen 2) I choose and see the record of choice,
    screen 3) I click submit,
    screen 4) update is confirmed
    screen 5) when I click "go to site", this is the result:
    screen1.png screen1.png screen2.png screen3.png screen4.png
     

    Attached Files:

    pshaw, Sep 13, 2022 IP
  9. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    Hi, the code below displays the records in a database table. I select a record from a dropdown and
    update a couple of fields in that record. So far so good.... Then I want to, using the "target"
    field (which is a url) as an argument, onclick or whatever and go to that url. Surely there's a way?

    <!DOCTYPE><html><head><title>email 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= 'target'>";
    echo '<option value="">'.'--- Select email 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
    $con=mysqli_connect("localhost","root","","homedb");
    if(mysqli_errno($con))
    {echo "Can't Connect to mySQL:".mysqli_connect_error();}
    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; }
    // $con->close();

    }
    ?>
    <p>
    <button onclick="myFunction('<?php echo $row["url"];?>')">go to url</button>
    <script language="javascript">
    var sid;
    function myFunction(sid) {
    window.open(sid);
    }
    </script>
    </body></html>
     
    pshaw, Sep 15, 2022 IP
  10. wmtips

    wmtips Well-Known Member

    Messages:
    598
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #10
    Your code looks very unprofessional.
    1. Why do you connect to mysql several times? You already have connection saved in the $con variable.
    2. You always update the same row with target='target'.
    3. Assuming you are outside of the loop you can't use the $row variable. You can move the <button> inside the if(isset($_POST['target'])) block and use $_POST['target'] or $name as your URL:
    <button onclick="myFunction('<?php echo $name;?>')">go to url</button>
     
    wmtips, Sep 16, 2022 IP
  11. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #11
    thank you so much. Finally a response I can work off of. No, I am not professional, just learning.
     
    pshaw, Sep 16, 2022 IP
  12. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #12
    Thanks for your help. I believe I followed your suggestions. My revised code is below and screenshots
    of the progressions follow. As you can see the redirections till doesn't work.

    <!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= '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>
     

    Attached Files:

    pshaw, Sep 16, 2022 IP
  13. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #13
    This query is not good...
    $sql = "UPDATE lookuptbl SET lastused=NOW(), count=$count + 1 WHERE target=$target";

    I think it should be:
    $sql = "UPDATE lookuptbl SET lastused=NOW(), count=count + 1 WHERE target='$target' limit 1 ";


    I don't know what you are trying to do, but if you want a simple redirect, then edit this line below:
    if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }

    change to:
    if ($con->query($sql) === TRUE) {
    echo "Record updated successfully";
    echo '<script type="text/javascript">
    window.location = "'.$target.'";
    </script>';
    exit;
    }


    What this will do:
    1. User selects something from dropdown.
    2. your code checks if entry is there in db for that submitted data
    3. If its there, then it updates db
    4. Redirects user to submitted target.
    All that table etc display is of no use.
    User will not see it. They will get redirected instead.

    If record is not there, then user sees "record not found" error message. This one:
    else { echo "Error updating record: " . $con->error; }
     
    JEET, Sep 17, 2022 IP
  14. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #14
    Thanks so much for your suggestions. so close. Below is the rewrite. looks like there's a lot of
    xcess code? All was 'til I clicked for redirect. Then I got:
    Not Found
    The requested URL was not found on this server.
    Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/7.4.28 Server at localhost Port 80
    -------------------------------------------
    It is searching for localhost/home/target

    <!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= '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));}

    $id="id";
    $target="target";
    $count="count";
    $lastused="lastused";
    $sql = "UPDATE lookuptbl SET lastused=NOW(), count=count + 1 WHERE target='$target' limit 1 ";
    if ($con->query($sql) === TRUE) {
    echo "Record updated successfully";
    echo '<script type="text/javascript">
    window.location = "'.$target.'";
    </script>';
    exit;
    }
    }
    ?>
    <p>
    <button onclick="myFunction('<?php echo $target;?>')">go to url</button>
    <?php
    ?>
    </body></html>
     
    pshaw, Sep 18, 2022 IP
  15. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #15
    What is the target value in the dropdown box?
     
    JEET, Sep 18, 2022 IP
  16. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #16
    the target I selected at that was "http//:netflix.com". I tried several b4 I posted, confirmed the target existence, spelling. I'm thinking I removed too much?
     
    pshaw, Sep 18, 2022 IP
  17. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #17
    Try this code:


    <!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>
    <input type="submit" name="submit" value="Submit"/>
    </form>

    <?php
    if(isset($_POST['target']))
    {
    $name = addslashes($_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)); exit;}

    $id="id";
    $target="target";
    $count="count";
    $lastused="lastused";
    $sql = "UPDATE lookuptbl SET lastused=NOW(), count=count + 1 WHERE target='$target' limit 1 ";
    if ($con->query($sql) === TRUE) {
    echo "Record updated successfully";
    echo '
    <button onclick=" window.location=\''. $target. '\'; break; "> Open URL '.$target.' </button>
    ';
    exit;
    }else{ echo "Record not found in db"; exit; }

    }
    ?>
    </center></body></html>
     
    JEET, Sep 18, 2022 IP
  18. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #18
    oops, this code offers the menu, displays the selected, says it updated (fibs) PERIOD. nada.
     
    pshaw, Sep 19, 2022 IP
  19. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #19
    you've given me more hope. I put the table stuff back in 'cause I want to see the info.
     
    pshaw, Sep 19, 2022 IP
  20. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #20
    Why are you trying to use JavaScript to turn a <button> into an <a>nchor? Why not just use an anchor in the first flipping place?

    Because this:

    <button onclick="myFunction('<?php echo $row["url"];?>')">go to url</button>
    Code (markup):
    Is ignorant garbage doing the job of:

    <a href="<?= htmlspecialchars($row["url"]) ?>')">go to url</a>
    Code (markup):
    Though if you want to actually learn, you need to stop dicking around with PHP and JavaScript when you don't even know enough HTML to be doing a single blasted thing in web development. Your markup is the worst of mid-90's practices and until you get at LEAST to the point of 1998 practices, you're going to continue to struggle with broken, nonsensical code and a result that is useless to the majority of potential users.

    Such as the use of tags like <center> that have been invalid HTML for 24 years, use of paragraph tags before things that aren't paragraphs and have no closings, etc. etm.

    Of course this is also another case for why I would suggest you forget mysqli_ even exists since it's more of a crutch for people who cannot get their head out of the old mysql_ garbage, than it is a modern development methodology.

    Don't even get me started about inane outdated asshattery like:

    WHERE target = '".$name."'";

    It's called "prepare/exec", use it. Addslashes does dick for security.

    Note I'm guessing WILDLY here, and there may be typos, but...

    <!DOCTYPE><html><head>
    
    	<meta charset="utf-8">
    	<meta name="viewport" content="width=device-width,initial-scale=1">
    	
    	<link rel="stylesheet" href="screen.css" media="screen">
    	
      <title>lookup menu</title>
      
    </head><body>
    
    <?php
    
    try {
    
    	$db = new PDO(
    		'mysql:dbname=homedb;host=localhost',
    		'root', // username
    		'' // password
    	);
    
    } catch (PDOException $e) {
    	die('Error!: ' . $e->getMessage());
    }
    
    echo '
    	<form method="post" action="">
    		<fieldset>
    			<label>
    				Account:<br>
    				<select>';
    			
    $result = $db->query('
    	SELECT *
    	FROM lookuptbl
    ');
    
    while ($row = $result->fetch()) echo '
    					<option>', $row['target'], '</option>';
    				
    echo '
    				</select>
    			</label><br>
    			<button>Submit</button>
    		</fieldset>
    	</form>';
    
    if (isset($_POST['target'])) {
    	$execData = [ ':target' => $_POST['target'] ];
    	$result = $db=>prepare('
    		SELECT target
    		WHERE target = :target
    	');
    	$result->exec($execData);
    	if ($target = $result->fetch()) {
    		$update = $db->prepare('
    			UPDATE lookuptbl
    			SET
    				lastused = NOW(),
    				count = count + 1
    			WHERE target = :target
    			LIMIT 1
    		');
    		$update->exec($execData);
    		echo '
    			<p>', (
    				$update->rowCount() ?
    				'Record updated successfully' :
    				'Record not found'
    			), '</p>';
    	}
    	$targetURL = htmlspecialchars($target['target']);
    	echo '<a href="', $targetURL, '">Open URL ', $targetURL, '</a>';
    
    }
    
    ?>
    
    </body></html>
    Code (markup):
    Is more along the lines of what you should be writing. You want things like centering, put it in the external stylesheet not your bloody markup! What things look like has ZERO business in your HTML whatsoever!

    -- edit -- changed the listing to fix where I had a backtick instead of regular quote.
     
    Last edited: Sep 20, 2022
    deathshadow, Sep 20, 2022 IP