0

Here is my PHP block. The get functions all work fine.

<?php 
function getTitle()
{
    $connection = new PDO('mysql:host=x.x.x.x;dbname=x;charset=utf8', 'x', 'xxxx');
    $query      = $connection->query("SELECT `value` FROM `services` WHERE `type` = 'title';");
    $data     = $query->fetchAll(PDO::FETCH_COLUMN);
    echo '<textarea id="services-title" name="services-title" rows="1">' . htmlentities($data[0]) . '</textarea>';
}

function submitData()
{
    $connection = new PDO('mysql:host=x.x.x.x;dbname=xx;charset=utf8', 'xx', 'xxxx');
    if (isset($_POST['submit'])) {
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-description'] . "' WHERE `type`='description';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-meta_keywords'] . "' WHERE `type`='meta_keywords';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-title'] . "' WHERE `type`='title';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . htmlspecialchars($_POST['services-content']) . "' WHERE `type`='content';");
        $query->execute();
    }
}

And here is my form.

<form method="post">
<h2>Metadata</h2>
<h3>Description</h3>
<?php
<h3>Title</h3>
<?php
getTitle();
?>
<div class="flex-row-right">
<input type="submit" name="submit" value="submit" onclick="submitData()" />
</div>
</form>

All I need is for my forms submit button to execute the submitData() function to update my database. What am I doing wrong?

mowsie2k
  • 45
  • 8
  • see above-linked question for more on this, but in essence: since PHP runs on the *server*, the only way to make PHP code run on click is to have clicking the button send a new request to your server. – Robin Zigmond Sep 20 '22 at 19:38
  • onClick="" on the button will invoke a JavaScript function. After you submit the form, you need to check on the PHP side, that if a POST request is made and then execute your code to handle the submitted data. – Rakesh Mehta Sep 20 '22 at 19:39

2 Answers2

2

Assuming that the PHP part and the HTML part is on the same file (lets say index.php), then your code should look like this..

<?php 
function getTitle()
{
    $connection = new PDO('mysql:host=x.x.x.x;dbname=x;charset=utf8', 'x', 'xxxx');
    $query      = $connection->query("SELECT `value` FROM `services` WHERE `type` = 'title';");
    $data     = $query->fetchAll(PDO::FETCH_COLUMN);
    echo '<textarea id="services-title" name="services-title" rows="1">' . htmlentities($data[0]) . '</textarea>';
}

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $connection = new PDO('mysql:host=x.x.x.x;dbname=xx;charset=utf8', 'xx', 'xxxx');
    if (isset($_POST['submit'])) {
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-description'] . "' WHERE `type`='description';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-meta_keywords'] . "' WHERE `type`='meta_keywords';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-title'] . "' WHERE `type`='title';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . htmlspecialchars($_POST['services-content']) . "' WHERE `type`='content';");
        $query->execute();
    }
}
?>

<form method="post">
<h2>Metadata</h2>
<h3>Description</h3>
<h3>Title</h3>
<?php getTitle(); ?>
<div class="flex-row-right">
<input type="submit" name="submit" value="submit" />
</div>
</form>
Rakesh Mehta
  • 519
  • 2
  • 9
2

You can't call php method using onclick="submitData()". This can only work with JavaScript. You need to add this at the top of your php file:

if(!empty($_POST['submit']) {
  submitData();
}

Here is a code:

<?php 
function getTitle()
{
    $connection = new PDO('mysql:host=x.x.x.x;dbname=x;charset=utf8', 'x', 'xxxx');
    $query      = $connection->query("SELECT `value` FROM `services` WHERE `type` = 'title';");
    $data     = $query->fetchAll(PDO::FETCH_COLUMN);
    echo '<textarea id="services-title" name="services-title" rows="1">' . htmlentities($data[0]) . '</textarea>';
}

function submitData()
{
    $connection = new PDO('mysql:host=x.x.x.x;dbname=xx;charset=utf8', 'xx', 'xxxx');
    if (isset($_POST['submit'])) {
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-description'] . "' WHERE `type`='description';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-meta_keywords'] . "' WHERE `type`='meta_keywords';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . $_POST['services-title'] . "' WHERE `type`='title';");
        $query->execute();
        $query = $connection->query("UPDATE `services` SET `value`='" . htmlspecialchars($_POST['services-content']) . "' WHERE `type`='content';");
        $query->execute();
    }
}

if(!empty($_POST['submit']) {
submitData();
}
?>

<form method="post" action="">
<h2>Metadata</h2>
<h3>Description</h3>
<?php
<h3>Title</h3>
<?php
getTitle();
?>
<div class="flex-row-right">
<input type="submit" name="submit" value="submit"/>
</div>
</form>