I have a simple form with one input field and one submit button. When i click submit, i get error
This is the query in php. Query:
//Using MySQLi
$stmt = $con->prepare("INSERT INTO `emailsubscribe`
(email,medium,country)VAlUE(?,?,?)"); // Use prepared statements.
$stmt-> bind_param("sss", $email, $medium, $country);
$stmt-> execute();
This table has 3 columns email, medium and country.
$('#formoid').on('submit', function() {
$.ajax({
type: "POST",
url: "subscribe.php",
data: $(this).serialize(),
success: function(data){
$('.message').html(data).fadeIn();
}
});
return false;
});
<div class="message" style="color:black;"></div>
<form action="subscribe.php" title="" method="post" id="formoid">
<input type="email" id="email" name="email" minlength="7" size="40" placeholder="Enter your email here.." required><br><br>
<input type="hidden" name="medium" value="subbox" />
<input type="hidden" name="country" value="<?php echo $country; ?>" />
<input type="submit">
</form>
2
Answers
First, let’s wrap up the HTML data.
AJAX code below takes the form details and sends to
subscribe.php
. Note thatdocument.subscribeForm
below takes your form field variables and stores in the form. For this onlyname
value in HTML part is enough. Hence, I have not added anyid
field in the HTML form fields.Once the data is sent to
subscribe.php
, it’s now time to process it.This is the proper way how you should process your forms.
Firstly I don’t see any medium or country in your form as inputs. So I changed your HTML code
Then in your
subscribe.php
do the following. Take note, I just copied your exact SQL code. Use prepared statements or PDO to avoid SQL injection