Simple Form does not work - php

Trying to make a simple form to send an email to a database. New to php. The isset doesn't trigger so i guess the post isn't posting?! Heres what i have tried. I have tried with the get/post require etc. Thanks for any help.
<form class='form-horizontal' action='includes/news-letter.php' method='POST'>
<div class='form-group'>
<div class='col-xs-8 col-xs-offset-2'>
<label for='' class='control-label'>Email</label>
<input type='text' class='form-control input-newsletter' name='newsletterEmail' id='inputPassword' placeholder='someone#somwhere.com'>
</div>
</div>
<div class='form-group'>
<div class='col-xs-6 col-xs-offset-4'>
<button type='submit' class='btn btn-default btn-newsletter' name='newsletterEmail'>Get News Letter</button>
</div>
</div>
</form>
<?php
require_once("dbConnection.php");
news_letter();
function news_letter(){
// if(isset($_POST["newsletterEmail"]))
if (isset($_POST["newsletterEmail"]) && !empty($_POST["newsletterEmail"])){
// print_r($_POST);
echo "IS SET";
$newsletterEmail = $_POST['newsletterEmail'];
echo $newsletterEmail;
if ($newsletterEmail==''){
echo "<h6 class='alert alert-danger'>Please fill in field.</h6><br>";
}else{
$query = "INSERT INTO newsletterEmail ";
$query .= "(newsletterEmail)";
$query .= "VALUES (:newsletterEmail)";
$ps = $db->prepare($query);
$ps->execute(array(
"newsletterEmail" => $newsletterEmail,
));
}
}
};
?>

Your submit button has the same name that your textfield, that why it doesn't work.
You don't have to check either "isset" and "!empty" (not empty) in fact if the value is set it's not empty so just do this :
if(!empty($_POST["newsletterEmail"])){
echo "IS SET";
}

Related

When Clicked edit button, corresponding details must be filled to the form for edit

I have a form which can submited.and have list,that listed all submited form details.
I tried it in different ways.I want to fill the form with the corresponding details when I clicked the edit button.
Here is my php file
<div class="row">
<div class="col-sm-9">
<b>Leader Name : </b><?php echo($row["lead_name"]); ?><br>
<b>Phone Number : </b><?php echo($row["phone_number"]); ?><br>
<b>Email : </b><?php echo($row["email"]); ?><br>
<b>Part Created Time : </b><?php echo($row["create_date_and_time"]); ?>
<br>
</div>
<div class="col-sm-3 ">
<form role="form" action='index.php' method='POST'>
<input type='hidden' name='party_id' value='<?php echo($row["party_id"]); ?> '>
<input type="submit" class="btn btn-sm btn-success btn-block" id="edit" name="edit" value="Edit" style="font-size: 12px; padding: 3px;">
<input type="submit" class="btn btn-sm btn-danger btn-block" id="delete" name="delete" value="Delete" style="font-size: 12px; padding: 3px;">
</form>
<?php
if (isset($_POST['delete'])) {
print("<script> alert('delete'); </script>");
$party_id = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$queryDelete = "DELETE FROM party_details WHERE party_id='$party_id'";
if ($conn->query($queryDelete)) {
$_SESSION['party'] = "";
$_SESSION['trips'] = [];
print("<script>
alert('Party removed');
window.location.href='../tripCreate';
</script>");
} else {
print("<script>alert('Error when remove ! ');</script>");
}
$_POST = array();
}
if (isset($_POST['edit'])) {
$party_id1 = isset($_POST['party_id']) ? $_POST['party_id'] : "";
$query1 = "SELECT * FROM party_details WHERE party_id='$party_id1'";
$result1 = $conn->query($query1);
$row1 = $result1->fetch_assoc();
}
?>
</div>
first of all, you should specify not only result you want to achieve, but also what kind of problem you are facing.
is it php error, or information not being displayed in resulted page?
one thing i spotted is that you got $row1 = $result1->fetch_assoc(); but in form you echo $row[] (instead of $row1[]), which i dont see being created anywhere.
also, did you try var_dump($row) in php and check its content (or $row1...)?

Changing form fields with ajax but field values not passing on submit

I have a standard form with a drop down list of customers, using ajax when a customer is selected it calls the load-customers.php page and changes the form fields entirely based on customer_id whilst filling it with the relevant information.
One of these fields is another drop drown called address_id which again allows the user to select from multiple addresses already defined for the selected customer.
The issue is, address_id is not being passed to $_POST on submit, to be fair none of the other fields are either... but the other fields are just placeholders and i only care about the customer_id and address_id.
How do i get around this? This is the only bit of ajax I've ever used.
ajax:
function getCustomer(val) {
$.ajax({
type: "POST",
url: "load-customers.php",
data:'id='+val,
success: function(data){
$("#customer").html(data);
}
});
}
customer:
<div class='form-group row'>
<label class='col-md-4 control-label'>Quick Select</label>
<div class='col-md-8 selectContainer'>
<div class='input-group'>
<span class='input-group-addon'><i class=''></i></span>
<select name='Customer_ID' class='form-control selectpicker' id='Customer_ID'
onChange="getCustomer(this.value);">
<option value='0'>Please create a new Customer</option>
<?php
$sell_type = "Trade";
$getcustomer = $conn->prepare("SELECT * FROM customers WHERE type = :type");
$getcustomer->bindParam(':type', $sell_type);
$getcustomer->execute();
foreach ($getcustomer->fetchAll(PDO::FETCH_ASSOC) as $row) {
$customer_id = $row['id'];
$customer_name = $row['name'];
$customer_company = $row['company'];
if (!empty($customer_company)) {
$customer = $customer_company;
} else {
$customer = $customer_companyname;
}
echo "<option value='$customer_id'>$customer</option> ";
}
?>
</select>
</div>
</div>
</div>
<div id='address'>
// address results go here
</div>
address:
<div class='form-group row'>
<label class='col-md-4 control-label'>Postcode</label>
<div class='col-md-8 selectContainer'>
<div class='input-group'>
<span class='input-group-addon'><i class=''></i></span>
<select id='address_id' class='form-control selectpicker' name='address_id' >
";
$getaddresses = $conn->prepare("SELECT * FROM addresses WHERE customer_id = :id");
$getaddresses->bindParam(':id', $customer_id);
$getaddresses->execute();
foreach ($getaddresses->fetchAll(PDO::FETCH_ASSOC) as $row) {
$address_id = $row['id'];
$postcode = $row['postcode'];
$areacode = $row['areacode'];
echo "
<option value='$address_id'>$postcode</option>
";
}
echo "
</select>
</div>
</div>
</div>
The form itself is quite large but the header is:
<form class="form-horizontal justify-content-center" action="new.php" method="post" id="form">
and submitted with:
<button type="submit" class="btn btn-primary mb-2">Submit</button>

html form post is not sending variable data

After clicking "delete" (submit): I can tell that it processes the form because I get the error "You forgot to select a vendor to delete.".
The form itself is generated using PHP. When I check the HTML output, I see that the values are there. But when it gets to POST, nothing carries over. I'm testing and using a local host, so other than the existing action link, I'm not sure what else I can modify.
I have the exact same code in another delete form and it works fine. I literally copied the other form to create this one and just changed the SQL/Variable names.
PHP Code:
#delete vendor
require('includes/mysqli_connect.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$errors = array(); //array to collect errors
//check to ensure fields are filled out
if (!empty($_POST['vendor'])) {
$vendor = trim($_POST['vendor']);
}
else {
$errors[] = "<p>You forgot to select a vendor to delete</p>";
}
if (empty($errors)) {
//insert information into appt table
$query = "UPDATE vendors_t SET active = 0 WHERE vendorID = $vendor;";
$result = mysqli_query($dbc, $query);
if($result){
echo '<div class="alert alert-success" role="alert">';
echo '<h2>Thank you!</h2>';
echo '<p> Your request to delete vendorID ' . $customerID . 'has been completed </p></div>';
} else {
echo "Error updating record: " . mysqli_error($dbc);
}
} else{
echo '<div class="alert alert-danger" role="alert"><br>';
echo '<h2>Errors Detected</h2>';
echo '<p>The following errors occured:<br/>';
foreach ($errors as $msg) {
echo "$msg<br/>\n";
}
echo '</p><p>Please try again. </p></div>';
}
}
HTML Code
<div class="container standout" >
<div class="col-lg-12">
<h2>Delete a vendor</h2>
<p> Please select a vendor to delete.</p>
</div>
<div class="row">
<form action="index.php?pagelet=deletevendor" method="post">
<div class="form-group col-md-4">
<label for="vendor">Vendors</label>
<select name="vendor" class="form-control">
<option name="vendor" selected disabled>Choose here</option>
<?php
$query = "SELECT vendorID, contactFname, contactLname, businessName FROM vendors_T WHERE active = 1;";
$result = mysqli_query($dbc, $query);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<option name="vendor" value="'.$row['vendorID'].'">'
.$row['vendorID'].' '.$row['businessName']
.' '.$row['contactFname'].' '.$row['contactLname']
.'</option>';
}?>
</select>
<br>
<br>
<input class="btn btn-primary" id="submit" type="submit" value="Delete">
</div>
</div>
</form>
</div>
Your option names should be the value of the vendor ID (I presume) and certainly not "vendor".
removing name="vendor" from options & removing selected both work.
After removing name="vendor" - I added selected back and it still works.
Thank you!!

php: get post results from multiple forms through one submit

I have a table with a list of clients, then a "modify" button then a "delete" button, and then a "add to mailing list" checkbox, each of these elements is its own form. There is also a selector at the end to chose which mailing list to add the address to.
My problem is with these checkboxes.
I have one submit button at the end of the table to add all of the checked emails to a file, but it only works on the last email address.
Here is the code for the checkboxes:
echo "<td>";
echo "<form action=\"\" method=\"post\">";
if($email!="" && $email!=null){
echo "<label class=\"checkbox-inline\">
<input type=\"checkbox\" name=\"mailing[]\" value=\"".$email[0][0]."\" title=\"".$email[0][0]."\"><span><i class=\"glyphicon glyphicon-envelope\"></i></span></label>";
}else{
echo "<label class=\"checkbox-inline\">
<input type=\"checkbox\" disabled><span><i class=\"glyphicon glyphicon-envelope\"></i></span></label>";
}
echo "</form>";
echo "</td>";
//... more code
//selector + submit
<form action="" method="post">
<div class="col-sm-6 pull-right">
<label class="control-label">Ajouter ces adresses à la liste:</label>
<select class="form-control" name="liste">
<option value=""></option>
<?php
$linkpdo = openConnection();
$req = $linkpdo->prepare('SELECT * FROM AERA.mailing');
$req->execute();
$lists = $req->fetchAll();
if(count($lists)<>0){
for ($i = 0; $i < count($lists) ; $i++){
echo "<option value=\"".$lists[$i][0]."\">";
echo $lists[$i][1];
echo "</option>";
}
}
?>
</select>
</br>
<button type="submit" class="btn btn-info" id="clickAll">Ajouter</button>
How can I get all of the checkbox values? Or is there another way to go about it? Thanks
UPDATE
Here is how I am currently handling the results:
if(!empty($_POST['mailing'])) {
foreach ($_POST['mailing'] as $key => $value){
$mailing=$_POST['mailing'][$key];
if(stripos(file_get_contents($filename), $mailing)!== false){
echo '<div class="container">
<div class="col-sm-6 col-sm-offset-3 alert alert-info">
<p class="text-center">Vous avez essayé d\'insérer des doublons.</br>Ces valeurs n\'ont pas été prises en compte</p>
</div>
</div>';
}else{
$stringIn.=$mailing.", ";
}
}
}
Why don't you make a html form and than a php script?
you can see an example here: Getting checkbox values on submit

POST and GET at the same time

hello everyone sorry but am just beginner in php , mysql ... i was developing question and answer website which i have page for all Question (Q.php) and page for displaying a specific question (QR.php) and get information according to data sent from Q.php via url ($_GET['start'] i also have page to confirm that the answer is already submitted .... but i got error when entering the id from get method and the message from post method ... any answer will be appreciated
Q.php
<?php
include("pagination.php");
if(isset($res))
{
while($result = mysql_fetch_assoc($res))
{
echo '<div class="shop-item">' ;
echo ' <div class="price">' ;
echo $result['Inquirer'] ;
echo ' </div>' ;
echo ' <div class="price">' ;
echo $result['question'] ;
echo ' </div>' ;
echo ' <div class="actions"> ';
echo '<input type="button" class="btn btn-large " value="More Info" onclick="window.location=\'QR.php?start=' . urlencode($result['id']) . ' \';" />';
echo '</div> ';
echo ' </div> ';
}
}
?>
QR.php
<form action="QRR.php" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name ="answer" id="Test">
</textarea>
<br>
<div class="actions">
<?php echo '<input type="button" class="btn btn-large " value="Post Answer" onclick="window.location=\'QRR.php?start=' . urlencode($_GET['start']) . ' \';" />'; ?>
</div>
</div>
</div>
</form>
QRR.php
<?php
// variables
$answer=$_REQUEST['answer'];
require ("coonection.php");
$FURL = $_REQUEST['hi'];
//query
$query = "INSERT INTO `answers`(`answer_id`, `question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('','$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data)
{
echo "Your Questions Has Been Successfully Added ";
}
?>
if i removed passing hi from QR to QRR answer stored //$answer
if i removed storing answer from the text area the id from url stroed //$FURL
This isnt the most beautiful code, as i just copied yours and made a few modifications.. but this form will submit back to the same page, and the php will run and insert ONLY if the form is submitted.
if(isset($_POST['answer'])) says "if the variable _POST answer is set, run the php code and insert.. if it is not set, do nothing.
You will notice the form action is left blank, you can set it to the page name yo are on.. as the form will send the variables to the same page. This is a good way of doing it because if there are errors, you can prepopulate the input or textareas with the code they just typed in.
<?php
// variables
if(isset($_POST['answer'])){
$answer=$_POST['answer'];
require ("coonection.php");
$FURL = $_POST['hi']; // there is no input name 'hi' set in your form. so this code will fail due to that.
//query
$query = "INSERT INTO `answers`(`question_id`, `answer`, `answerer`, `rate`, `dnt`) VALUES ('$FURL','$answer','ahmed','',CURRENT_TIMESTAMP)";
$data=mysql_query($query) or die(mysql_error());
if($data){
echo "Your Questions Has Been Successfully Added ";
}
}
?>
`
you will see i removed your answer_id. if you use this code, make sure that field is set to primary auto increment in your database.
<form action="" method="POST">
<div class="blog-post blog-single-post">
<div class="single-post-title">
<h2>Post Your Answer</h2>
</div>
<div align="Right">
<textarea class="form-control" rows="4" name="answer" id="Test"></textarea>
<br>
<div class="actions">
<button type="submit" class="btn btn-large " value="Post Answer">Post Answer</button>
</div>
</div>
</div>
</form>
NOTE: both of these snippets will go in the same page.

Categories