Jump to content

Is onclick possible?


Go to solution Solved by requinix,

Recommended Posts

Hello all,

 

          I am interested in creating a very simple form that onclick it adds a new record to my table.  Can I accomplish this task with the basic code that I have on the line with "Button Type" ?  I am totally open to different methods too.

<?php
$servername = "localhost";
$username = "bleh";
$password = "password";
$dbname = "logger";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception.
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // sql to select last record inserted.
    foreach($conn->query('select status from comins_and_goins order by id DESC limit 1') as $row) {
    $var = $row['status'];
}
    if ($var == 1) {
        echo "You are going to work or somewhere else for a while.";
        echo '<button type="button" onclick="MySQL query that inserts record.">Click Me</button>';
    }
    else{
        echo "You are back home!  I missed you!";
    }
}
catch (PDOExecption $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

  On a side note, I also feel that I need to acknowledge the foreach statement is a little redundant for the MySQL syntax that I am executing on the same line.  I figured, it works and I just want my new toy operational ASAP. ;)

Link to comment
https://forums.phpfreaks.com/topic/298397-is-onclick-possible/
Share on other sites

  • Solution

Javascript cannot interact directly with PHP code or with your database.

 

Use a form.

<form action="" method="post">
<button type="submit" name="comehome">Click Me</button>
</form>
then

if (isset($_POST["comehome"])) {
	// insert the record
}

// continue on as normal
It's not onclick because that requires AJAX and you said you wanted this thing done quickly. Which is a bad mentality for a programmer to have but whatever.
Link to comment
https://forums.phpfreaks.com/topic/298397-is-onclick-possible/#findComment-1522121
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.