Form not passing data to php file while using bootstrap - forms

I am trying to create a modal that will allow a user to enter some data and then have php send an email with this data.
I have a form in the modal and the php sends the email however I cannot seem to get/use the data within the form as it doesn't seem to recover anything when I use the $_POST. Can someone please help as this is driving me mad.
Code is listed below
<!-- Modal -->
<div class="modal fade" id="modalform" tabindex="-1" role="dialog"
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">
Submit your details to have a Sales Assistant call you back
</h4>
</div>
<!-- Modal Body -->
<div class="modal-body">
<form role="form" method="post" action="enquire.php">
<div class="form-group">
<label for="InputName">Your Name</label>
<input type="text" class="form-control"
id="InputName" placeholder="Your Name"/>
</div>
<div class="form-group">
<label for="InputCar">Car Type</label>
<input type="text" class="form-control"
id="InputCar" placeholder="Car you are interested in"/>
</div>
<div class="form-group">
<label for="InputContact">Phone Number</label>
<input type="text" class="form-control"
id="InputContact" placeholder="A number for us to contact you on"/>
</div>
<button type="submit" class="btn btn-default" value="submit">Submit</button>
</form>
</div>
<!-- Modal Footer -->
<div class="modal-footer">
<h4>Alternatively you can call us on </h4>
</div>
</div>
</div>
php
<?php
$InputName = $_POST['InputName'];
$InputCar = $_POST['InputCar'];
$InputContact = $_POST['InputContact'];
$from = "made up email address";
$to = "another made up email address";
$subject = "Enquiry from web page";
$message = "$InputName is interested in $InputCar. Contact them on $InputContact";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $from \r\n";
$headers .= "Return-Path: $from\r\n";
$headers .= "X-Mailer: PHP \r\n";
mail($to, $subject, $message, $headers);
if(mail($to,$subject,$message,$headers))
{
echo "Mail sent OK";
}
else
{
echo "Error sending email!";
}
?>

Your forms were all wrong.. This is how the form should be.
<form action="" method="post">
<div class="form-group">
<label>Your Name</label>
<input type="text" class="form-control" name="name" placeholder="Your Name">
</div>
<div class="form-group">
<label>Car Type</label>
<input type="text" class="form-control" name="car" placeholder="Car you are interested in">
</div>
<div class="form-group">
<label>Phone Number</label>
<input type="text" class="form-control" name="phone" placeholder="A number for us to contact you on">
</div>
<input type="submit" class="btn btn-default" value="submit" name="send">
</form>
Your PHP should then be:
if (isset($_POST['send'])) {
$InputName = $_POST['name'];
$InputCar = $_POST['car'];
$InputContact = $_POST['phone'];
// do code
}
EDIT: under form action="" you need to add your PHP file again.

Related

why form not submit in laravel 5.4

i have a form for User Registration, it was working perfect before but now its not submit and doesn't get any error. i tried to show all my post data but its not post any data.. please help
<form role="form" id="reg-form" method="post" class="form-horizontal" action="{{ url('/create_user') }}">
{{ csrf_field() }}
<h2>Create Account</h2>
<div class="form-group">
<div class="col-sm-6" id="user-firstname">
<input type="text" class="form-control" id="firstName" name="firstName" placeholder="First Name" required="required">
</div>
<div class="col-sm-6">
<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" id="displayName" name="displayName" placeholder="Choose your display name" required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="email" class="form-control" id="email" name="email" placeholder="Your Email" required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required="required">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="password" class="form-control" id="confirm_password" name="confirm_password" onkeyup="checkPass(); return false;" placeholder="Confirm Password" required="required">
<span id="confirmMessage" class="confirmMessage"></span>
</div>
</div>
<button type="submit" class="btn btn-primary btn-block">Sign in</button>
</form>
Route
Route::post('/create_user', 'Auth\RegisterController#createUser');
Method
public function createUser(Request $request)
{
dd($request->all());
}
I think you forgot to close some open tag above the form. It happened to me too.
Possible error is the "/"
<form role="form" id="reg-form" method="post" class="form-horizontal" action="{{ url('/create_user') }}">
so plz remove it like
<form role="form" id="reg-form" method="POST" class="form-horizontal" action="{{ url('create_user') }}"> {{-- '/' removed --}}
and also in
Route::post('create_user', 'Auth\RegisterController#createUser'); // '/' removed
It worked for me.
Replace {{ csrf_field }} with {!! csrf_field !!}}. It will work

Form in a modal box is not posting on submiit

I have a contact form in a modal box and a php file to process the data and send it as an email.
For the life of me I can't get the modal to send any information - it just closes as if nothing happened.
Here is the modal box code
<div id="quoteme" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
<div class="modal-dialog" role="document">
<div class="boxmod">
<div class="row">
<div class="modal-body">
<div class="col-sm-8 col-sm-offset-2 text-center bfh-form-modal">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">×</button>
<form id="myForm" action="" method="POST" action="formtest.php">
<h1>Quick Quote</h1>
<p>Contact us today, and get a reply with in 24 hours!</p>
<fieldset>
<input class="monicker" name="name" placeholder="Your name" type="text" tabindex="1" required>
</fieldset>
<fieldset>
<input placeholder="Your Email Address" name="email" type="email" tabindex="2" required>
</fieldset>
<fieldset>
<input placeholder="Your Phone Number" name="telnum" type="tel" tabindex="3" required>
</fieldset>
<fieldset>
<input placeholder="Your Web Site starts with http://" name="userurl" type="url" tabindex="4" required>
</fieldset>
<fieldset>
<textarea placeholder="Type your Message Here. To save time, please include as much detail as possible about the work you wish us to quote on." name="comments" tabindex="5" required></textarea>
</fieldset>
<fieldset>
<input name="submitbutton" type="submit" id="contact-submit" data-submit="...Sending" value="Submit"></input>
</fieldset>
</form>
</div>
</div>
</div>
</div>
</div>
What am I missing to make this send information to formtest.php?

Bad Request/form not defined on modal form submit to email - flask

I am trying to submit a modal form with various input data to my email through my flask app, but am getting 400 bad request error or "form is not defined" error because I tried using this line in the function:
form = modal1form(request.form)
Not sure whats wrong here - note: modal is located at /dashboard/projects/#modal1
Thanks
# new project function
#app.route('/dashboard/projects/#modal1form/', methods=["GET","POST"])
def new_project():
try:
if request.method == "POST":
if form.validate() == False:
flash('Invalid! All fields are required.')
return redirect(url_for('new_project'))
else:
msg = Message(request.form['name'], sender='request.form["email"]', recipients=['myemail#gmail.com'])
msg.body = """
From: %s %s \n
%s \n \n %s \n \n %s \n %s \n
%s
""" % (request.form['wtype', 'wpack', 'bts', 'fts', 'comments'])
mail.send(msg)
flash('Congratulations! A new project has been submitted.')
gc.collect()
return render_template('dashboardProjects.html')
elif request.method == 'GET':
return render_template("dashboardProjects.html")
except Exception as e:
return(str(e))
modal form
<div id="modal1" class="modal fade">
<form method="post" action="{{ url_for('new_project') }}" role="form" id="modal1form" name="modal1form">
<div class="form-group text-center">
<label for="name" class="control-label text-center"> Name </label>
<input type="text" class="form-control" name="name" id="name" placeholder="Your Name">
</div>
<div class="form-group text-center">
<label for="email" class="text-center"> Email Address </label>
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email">
</div>
<div class="col-md-3 col-md-offset-1">
<div class="form-group"> <br>
<label for="wtype"> Website Type </label> <br>
<h5 class="form-check-inline">
<input class="form-check-input" type="checkbox" name="wtype" id="wtype1" value="Static"> Static
</h5>
<h5 class="form-check-inline">
<input class="form-check-input" type="checkbox" name="wtype" id="wtype2" value="Blog"> Blog
</h5>
</div>
</div>
<br>
<div class="col-md-4 col-md-offset-1">
<div class="form-group">
<label for="wpack"> Website Package </label> <br>
<h5 class="form-check-inline">
<input class="form-check-input" name="wpack" type="radio" id="wpack2" value="Blog"> Basic
</h5>
<h5 class="form-check-inline">
<input class="form-check-input" name="wpack" type="radio" id="wpack3" value="Ecommerce"> Standard <br>
</h5>
</div>
</div>
<li class="dropdown pull-right list-unstyled" aria-expanded="false" style="position: top;"> <a class="dropdown-toggle" data-toggle="dropdown" role="button" style="font-size: 14px;"> <span class="caret"></span> <label for="template"> <i class="fa fa-cube"></i> Template </label> </a>
<ul class="dropdown-menu list-unstyled">
<li><h5 class="text-center"> <i class="fa fa-cubes"></i> Templates</h5></li>
<li><input name="bts" type="radio"/> Basic Template 1</li>
<li><input name="bts" type="radio"/> Basic Template 2</li>
<li role="separator" class="divider"></li>
<li><input name="fts" type="radio"/> Footer 1</li>
</ul>
</li>
</div>
<div class="form-group text-center"> <br>
<label for="comments"> Other Details </label>
<textarea name="comments" class="form-control" id="comments" placeholder="Additional comments, ideas, requests, questions?"></textarea>
<p class="help-block"> </p>
</div>
</form>
</div>
<div class="modal-footer brick">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success" form="modal1form">Submit</button>
</div>
ERROR 1: form is not defined
If you want to use Flask-WTF, you must create a Form, like this:
from flask import Flask, render_template
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DateRrequired
app = Flask(__name__)
app.config['SECRET_KEY'] = 'a very secret string'
class MyForm(FlaskForm):
name = StringForm('Your Name', validators=[DataRequired()])
submit = SubmitField()
app.route('/', methods=['GET', 'POST'])
def index():
form = MyForm()
if form.validate_on_submit():
name = form.name.data
...
return render_template('index.html', form=form)
Then you need to render the form in HTML template:
<form method="post">
{{ form.name() }}
{{ form.submit() }}
</form>
ERROR 2: 400 Bad request
In Flask, if you get form data by request.form['name'], you must be ensure this input have value, that's why you get the 400 error. Instead, I recommend you to use request.form.get('name', default_value), you can set a default value (as second argument) to avoid 400 error.
More details:
flask.pocoo.org/docs/0.11/patterns/wtforms
Hope it will help you.
Maybe ERROR 3: Bootstrap Modal
You can't open a modal with anchor name, like example.com#MyModal, but other modal plugin can. Check my answer on another question about this.

Confirmation message in same page after submission

I buy a Dreamweaver template and it comes with a Form page.But when I submit the message it sends me to another page with only text in it.
So I want to change it, so it can display a message after the submit buttom.
This is the CONTACT FORM inserted in a HTML page:
<div class="col-sm-6 col-xs-12">
<div class="status alert alert-success contact-status"></div>
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendemail.php" role="form">
<div class="row">
<!-- Name Field Starts -->
<div class="col-md-6">
<div class="form-group">
<label for="name" class="sr-only">Nombre: </label>
<input type="text" class="form-control" name="name" id="name" required placeholder="NOMBRE">
</div>
</div>
<!-- Name Field Ends -->
<!-- Email Field Starts -->
<div class="col-md-6">
<div class="form-group">
<label for="email" class="sr-only">Correo Electrónico: </label>
<input type="text" class="form-control" name="email" id="email" required placeholder="EMAIL">
</div>
</div>
<!-- Email Field Ends -->
<!-- Phone No Field Starts -->
<div class="col-md-6">
<div class="form-group">
<label for="phoneno" class="sr-only">Teléfono: </label>
<input type="text" class="form-control" name="phoneno" id="phoneno" required placeholder="TELÉFONO (10 DÍGITOS)">
</div>
</div>
<!-- Phone No Field Ends -->
<!-- Message Field Starts -->
<div class="col-xs-12">
<div class="form-group">
<label for="message" class="sr-only">Mensaje: </label>
<textarea class="form-control" rows="8" name="message" id="message" required placeholder="MENSAJE"></textarea>
</div>
</div>
<!-- Message Field Ends -->
<div class="col-xs-12">
<input type="submit" class="btn btn-lg btn-block btn-secondary text-uppercase" value="Enviar Mensaje">
</div>
</div>
</form>
</div>
<!-- Contact Form Ends -->
And this is the sendemail.php
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Email sent!'
);
$name = #trim(stripslashes($_POST['name']));
$email = #trim(stripslashes($_POST['email']));
$phoneno = #trim(stripslashes($_POST['phoneno']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = 'myemail#domain.com';
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Phone NO: ' . $phoneno . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
?>
Hopping you can help me

Bootstrap submit button won't work

I have the below code where I'm trying to submit data from a form into my back-end. Whenever I press the button it doesn't do anything and it's supposed to route to the specified place. I have looked at several examples and still hasn't worked. Below is my form. Any suggestions?
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4 text-center">
<h1>Sign Up</h1>
<form action ="/adduser" method="POST" id="adduser">
<fieldset class="form-group">
<label for="InputUsername"> Username </label>
<input type="text" class="form-control" name="username" id="InputUsername" placeholder="Enter username">
</fieldset>
<fieldset class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email"id="exampleInputEmail1" placeholder="Enter email">
<small class="text-muted">We'll never share your email with anyone else.</small>
</fieldset>
<fieldset class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password"id="exampleInputPassword1" placeholder="Password">
</fieldset>
<button class="btn btn-default" form="signup" type="submit">Sign up</button>
</form>
</div>
</dv>
</div>
Here is my route
#app.route('/adduser', methods=["POST"])
def adduser():
print(request.form, file=sys.stderr)
username = request.form['username']
password = request.form['password']
email = request.form['email']
models.User.create_user(email, password, username)
login_user(user)
return redirect_url('index.html')
Your button has a form attribute of signup but your form has an id of adduser, so the button does not belong to the form. Try changing these values to match.