-3

I need to refactor this code, Im not a php developer so i dont understand the syntax thats apparent here, what i want is this button to activate the moment the page is loaded.

<?php

$h = '';

if ($newsermonstomove) {
    foreach ($newsermonstomove as $vettel) {
        $h .= "<div style=\"padding: 5px;\">";
        $h .= "<button id=\"btn_" . $vettel->ID . "\" class=\"btn btn-primary btn-xs\" onclick=\"doMe(" . $vettel->ID . ");\" >s3</button><span style=\"padding-left:5px;\">Channel: " . $vettel->ChannelID . ", Sermon: " . $vettel->ID . " " . $vettel->SermonTitle . "</span> <div style=\"display:none;color:blue;\" id=\"msg_" . $vettel->ID . "\"></div><br/>";
        $h .= "</div>";
    }
} else {
    $h = "<h3>No new sermons.</h3>";
}

echo $h;
?>

From what i understand, the onclick has an escape in it onclick=\"doMe(" . $vettel->ID . ");\" In HTML i know that with a button if you do onclick=doMe its a reference to a function, which i feel like is the same thing thats happening here with the escape keys in it. its making a reference to the function doMe, but i want the function to fire automatically when the page loads.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Austin Howard
  • 780
  • 4
  • 10
  • 24
  • `doMe()` is likely a JavaScript function, which you can [call on page load](https://stackoverflow.com/questions/3842614/how-do-i-call-a-javascript-function-on-page-load). – showdev Jun 09 '21 at 22:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233622/discussion-between-austin-howard-and-magnus-eriksson). – Austin Howard Jun 10 '21 at 15:26

2 Answers2

0

Ok, i was able to figure out the answer I needed using JQuery.

$("document").ready(function() {
    setTimeout(function() {
        $(".btn").trigger('click');
    },10);
});

essentially what this function does is wait for the page to load then after 10 milliseconds it targets all buttons on the screen with the class name btn and triggers the click functionality of the button, thus firing off the button on the page load.

Austin Howard
  • 780
  • 4
  • 10
  • 24
-2
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<?php

    if (!empty($newsermonstomove)) {
        
        $h = '';
        
        foreach ($newsermonstomove as $vettel) {
            $h .= '<div style="padding: 5px;">';
            $h .= '<button data-id="' . $vettel->ID . '" class="btn-vettel btn btn-primary btn-xs">s3</button><span style="padding-left:5px;">Channel: ' . $vettel->ChannelID . ', Sermon: ' . $vettel->ID . ' ' . $vettel->SermonTitle . '</span> <div style="display:none;color:blue;" id="msg_'.$vettel->ID.'"></div><br/>';
            $h .= '</div>';
        }
        
    } else {
        $h = '<h3>No new sermons.</h3>';
    }

    echo $h;
?>

<script>
    $(function() {
        
        $('.bt-vettel').on('click', function(e) {
            
            var id = $(this).data('id'); 
            doMe(id);
            
        });
    });
<script>

enter image description here

XTRUST.ORG
  • 3,280
  • 4
  • 34
  • 60
  • From their question: _"i want is this button to activate the moment the page is loaded."_ This code won't do that. It's another improvement syntax wise though, but it still won't change the behavior. – M. Eriksson Jun 09 '21 at 22:55
  • What does it mean: i want is this button to activate the moment the page is loaded? What action should be fired once page is fully loaded (Click on the button with a special id or multiple button)? – XTRUST.ORG Jun 09 '21 at 23:45
  • 2
    That's what the original question says. If you wonder what it means, ask the OP. It's important that you read the question and ask for clarification _before_ attempting to answer. – M. Eriksson Jun 10 '21 at 05:02