lundi 20 avril 2020

How to call an onclick function in PHP?

Making a website that lets people create and join events. All I am trying to do at the moment is when the user clicks on the "join event" button, their username and the event_id of the event they clicked to join are added to my database. I know I cant set the onclick of the button to a php function, but I am not sure how else to do it. I am dynamically printing out the events from the database in printEvent(), so I want to set the onclick to the 'Join Event' button in this function because I can easily grab that specific event id.

Here is my code:

function printEvent($event_id, $month, $day, $location, $event_name)
{
    $url = "eventDetail.php?item=" . urlencode($event_id);

    echo '<!-- layout each event !-->
    <div class="event-container">
        <div class="date-container">
            <p><span class="month">'.$month.'</span>
                <span class="day">'.$day.'</span></p>
        </div>
        <div class="detail">
            <h3>'.$event_name.'</h3>
            <h4>'.$location.'</h4>
            <a href="'.$url.'" class="button">Learn More</a>
            <a href="" onclick="joinEvent('.$event_id.')" class="button">Join Event</a>
        </div>
    </div>';
}

function joinEvent($event_id) {
    $user_name = $_SESSION['username'];
    $query = mysqli_query($conn, "insert into event_users values($event_id, $user_name')");
}

I am specifically working with the <a href="" onclick="joinEvent('.$event_id.')" class="button">Join Event</a> line and then the joinEvent function at the bottom. Obviously its giving me an error saying it cannot find the joinEvent function, however I dont really need to use javascript for this. All I want to do is grab the specific event's id so I can pass it to my database in the same file. any help is appreciated




Aucun commentaire:

Enregistrer un commentaire