Form insert mysql

Hello everyone:),

I’m new with kirby
I want to implement a simple newsletter funktionallity. I set up a form with name and email and want to deligate to php. its a simple insert in a mysql database.

out of kirby it works. when i implement it always delegates me to 404 not found. not sure how kirby works.

	<h3>Newsletter</h3>
	<p>Abonieren Sie unseren Newsletter!</p>
	
<form action="site/snippets/sections/Newsletter.php" method="POST">

    <div class="grid__column">

        <label>Vor- und Nachname (Pflichtfeld)</label>
        <input name="newslettername" type="text" class="form-field"/>

        <label>E-Mail-Adresse (Pflichtfeld)</label>
        <input name="newsletteremail" type="email" class="form-field"/>


    </div>
    <div class="grid__column">
			<br /><br />
        <input type="checkbox" name="terms" value="1"/>
        <label for="terms" class="terms">Mit dem Absenden Ihrer Anfrage erklären Sie sich mit der Verarbeitung Ihrer
            angegebenen Daten zum Zweck der Bearbeitung Ihrer Anfrage einverstanden. Weitere Informationen erhalten Sie
            auf der Seite <a href="datenschutz" class="meta-nav__link">Datenschutz</a></label>

        <?php echo csrf_field() ?>
        <?php echo honeypot_field() ?>

        <p id="message" class="error-messages"></p>

        <input type="submit" class="button" value="Senden">
    </div>
</form>

<?php 
$user = "";  
$password = "";  
$host = "localhost";  
$dbase = "";  
$table = "Newsletter";  
 
$newslettername= $_POST['newslettername']; 
$newsletteremail= $_POST['newsletteremail']; 

  
  
// Connection to DBase  
$dbc= mysqli_connect($host,$user,$password,$dbase)  
or die("Unable to select database"); 
 
//$query= "INSERT INTO $table  ". "VALUES ('$first_name', '$last_name', '$email')"; 
$query= "INSERT INTO Newsletter (Name, EMail) VALUES ('$newslettername', '$newsletteremail')";
 
 
mysqli_query ($dbc, $query) 
or die ("Error querying database"); 
 
echo 'You have been successfully added.' . '<br>'; 
 
mysqli_close($dbc); 
 
?> 

how can i realize that?

think its probably the wrong way or path for php.
where do i have to add?

You cannot call a snippet directly like that that. One way of achieving this would be via a route that handles your form data.

See: Routing | Kirby CMS

Your form action would then call this route. Note that for a post request you have to set the method of the route to POST.

thank u very much.

i edited the config php this way

[
			'pattern' => 'newsletterInsert',
			'method' => 'POST',
			'action'  => function () {
				// do something here
				// when the URL matches the pattern above
				$user = "";  
				$password = "";  
				$host = "localhost";  
				$dbase = "";  
				$table = "Newsletter";  
 
				$newslettername= $_POST['newslettername']; 
				$newsletteremail= $_POST['newsletteremail']; 

  
  
				// Connection to DBase  
				$dbc= mysqli_connect($host,$user,$password,$dbase)  
				or die("Unable to select database"); 
 
				//$query= "INSERT INTO $table  ". "VALUES ('$first_name', '$last_name', '$email')"; 
				$query= "INSERT INTO Newsletter (Name, EMail) VALUES ('$newslettername', '$newsletteremail')";
 
 
				mysqli_query ($dbc, $query) 
				or die ("Error querying database"); 
 
				echo 'You have been successfully added.' . '<br>'; 
 
				mysqli_close($dbc); 
			}
		]

and edited the form this way

<form action="/newsletterInsert" method="POST">

but still got delegated to site not found 404

i’m sry.
update…
the insert works but it seems that i got deligatet to tghe site newsletterInsert which doesn’t exist.
probably it schould stay on the sie and only gives a return. something like

// Return code 200 on success.
return Response::json([], 200);

If you want to do the form handling on the page with the form, you can do the handling in the controller of the page, instead and use the $page->url() as the form action.

Controller example: https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend#the-event-php-controller (of course, something completely different, but you should get the idea).

Or you leave it in the route, but then redirect from the route to your original page, storing the response you want to show on the page in the session.