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?
2
Answers
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..
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:
Here is a code: