Run php from a button

HI all,

I am really struggling here with something that is probably really simple.

I have the following code

 <?php 

include 'connection.php';
 
try {
 
    $conec = new Connection();
    $con   = $conec->Open();
 
    $sql = "INSERT INTO `user`(`name`, `email`) VALUES (:name,:email)";
    $pre  = $con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
 
    if($pre->execute(array(':name' => 'Name', ':email' => 'Email'))){
        echo "Successful";
    }
} catch (PDOException $ex) {
    echo $ex->getMessage();
}
 ?>
 <!DOCTYPE html>
 <html>
 <head>
 	<style type="text/css">
 	</style>
 </head>
 <body>
 <div>
 	<form>
 	<input type="text" name="Name" placeholder="Name"></input>
 	<input type="text" name="Email" placeholder="Email Address"></input>
 	<button type="submit" name="submit"></button>
 		
 	</form>
 </div>


 	<script type="text/javascript">
 		


 	</script>
 </body>
 </html>


All I am wanting to do is tell this bit of code to use the fields I have in the form for name and email and put their content into the database.

Currently it is just putting “name” and “email” into the database.

I would like this to run when the button is clicked.

Thanks in advance

check if your form is submitted, just use an if-condition on $_SERVER['REQUEST_METHOD'] or validate each required $_POST field to display error messages. then replace your executing values with the corresponding $_POST fields.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.