How to create a contact form using bootstrap and HTML - javascript

I'm trying to build a contact form as I play around with building a website, but I'm not sure I'm doing it right. I'm using a bootstrap template and have the contact form at the bottom of the page. I have a model representing my class and have a controller that, from what I've gathered online, should be handling the clicking of the button, but something is clearly wrong. My background is C++ and Java, so this is a different ballgame, so can anyone tell me what I need to do to get it working? Code below. Thank you!
HomeController.cs:
namespace WebPlay.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(ContactInfo ci)
{
try
{
MailMessage mail = new MailMessage();
mail.To.Add(ci.Email);
mail.From = new MailAddress("email#gmail.com");
mail.Subject = "New Request";
string userMessage = "";
userMessage = "<br/>Name :" + ci.FirstName + " " + ci.LastName;
userMessage = userMessage + "<br/>Phone No: " + ci.ContactInfoID;
userMessage = userMessage + "<br/>Message: " + ci.Comments;
mail.Body = userMessage;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
//SMTP Server Address of gmail
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("xxxxxxx", "xxxxxxx");
// Smtp Email ID and Password For authentication
smtp.EnableSsl = true;
smtp.Send(mail);
ViewBag.Message = "Thank you for contacting us.";
}
catch
{
ViewBag.Message = "Fail";
}
return View();
}
}
Index.cshtml:
#{
ViewBag.Title = "Home Page";
}
<!-- Navigation -->
<nav class="navbar navbar-custom navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand page-scroll" href="#page-top">
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right navbar-main-collapse">
<nav class="navbar navbar-dark bg-inverse">
<a class="page-scroll" href="#about">About</a>
<a class="page-scroll" href="#download">Download</a>
<a class="page-scroll" href="#contact">Contact</a>
</nav>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Intro Header -->
<header class="intro">
<div class="intro-body">
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1 class="brand-heading">Greetings...</h1>
<p class="intro-text">I'm Jon. This is my site.<br>Ya know what's up.</p>
<i class="fa fa-angle-double-down animated"></i>
</a>
</div>
</div>
</div>
</div>
</header>
<!-- About Section -->
<section id="about" class="container content-section text-center">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h2>About Me</h2>
<p>I'm a programmer, crossfitter, and taco lover. The last 2 don't go togther so well.</p>
</div>
</div>
</section>
<!-- Download Section -->
<section id="download" class="content-section text-center">
<div class="download-section">
<div class="container">
<div class="col-lg-8 col-lg-offset-2">
<h2>Download Grayscale</h2>
<p>You can download Grayscale for free on the preview page at Start Bootstrap.</p>
Visit Download Page
</div>
</div>
</div>
</section>
<!-- Contact Section -->
<section id="contact" class="container content-section text-center">
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<h2>3...2...1...Contact Me</h2>
<p>Feel free to contact me for a quote or to just say hello!</p>
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">First Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Last Name:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" value="">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="email" name="email" value="">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email:</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" value="">
</div>
</div>
<div class="form-group">
<label for="exampleSelect1">Reason for contacting me:</label>
<select class="form-control" id="exampleSelect1">
<option>Quote on work</option>
<option>Just for fun</option>
<option>I might have a job for you</option>
</select>
</div>
<div class="radio">
<label><input type="radio" name="optradio">I need a web app</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio">I need a Windows app</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio">I need a phone app</label>
</div>
<div class="radio">
<label><input type="radio" name="optradio">I need nothing</label>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message:</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
<button class="btn btn-danger" type="reset">Reset</button>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
</div>
</div>
</section>
<!-- Map Section -->
<div id="map"></div>
<!-- Footer -->
<footer>
<div class="container text-center">
<p>Copyright © Your Website 2014</p>
</div>
</footer>
ContactInfo class:
namespace PellegriniP3_1.Models
{
public class ContactInfo
{
public string Comments { get; set; }
public int ContactInfoID { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsPhoneApp { get; set; }
public bool IsWebApp { get; set; }
public bool IsWindowsApp { get; set; }
public string TypeOfRequest { get; set; }
}
}

<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
Please check this link:-https://bootstrapbay.com/blog/working-bootstrap-contact-form/

Please check demo from following url, its working :
1.https://bootstrapious.com/p/how-to-build-a-working-bootstrap-contact-form

Related

Modal does not close after clicking button

im trying to build a modal, with a try-catch block, so if everyting is valid and entered correctly in the form, the modal should be closed
This is the function login, i wonder why $('#dialog').modal('hide'); does not work
<script>
function login() {
var kontakt = new Kontakt();
kontakt.name = document.getElementById('name').value;
kontakt.email = document.getElementById('email').value;
kontakt.plz = document.getElementById('plz').value;
kontakt.ort = document.getElementById('ort').value;
kontakt.strasse = document.getElementById('strasse').value;
try {
kontakt.pruefe();
}
catch(err) {
return window.alert(err.message);
}
kontakteSpeicher.neuerKontakt(kontakt);}
$('#dialog').modal('hide');
</script>
</head>
This is the modal
<div class="container">
<h2></h2>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#dialog">
Login-Dialog öffnen
</button>
<!-- The Modal -->
<div class="modal" id="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Login (im header)</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="container">
<p>im body:</p>
<form>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="Name" id="name">
</div>
</div>
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">E-Mail</label>
<div class="col-sm-10">
<input type="email" class="form-control" placeholder="E-Mail" id="email">
</div>
</div>
<div class="form-group row">
<label for="plz" class="col-sm-2 col-form-label">PLZ</label>
<div class="col-sm-10">
<input type="number" class="form-control" placeholder="PLZ" id="plz">
</div>
</div>
<div class="form-group row">
<label for="ort" class="col-sm-2 col-form-label">Ort</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="ort" placeholder="Ort">
</div>
</div>
<div class="form-group row">
<label for="strasse" class="col-sm-2 col-form-label">Strasse und Hausnummer</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="strasse" placeholder="Strasse und Hausnummer">
</div>
</div>
</form>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary btn-block" onclick="login()">Speichern</button>
</div>
</div>
</div>
</div>
</div>
The only problem you have is that the $("#dialog").modal('hide') statement isn't inside your login function body.
This should work.
function login() {
var kontakt = new Kontakt();
kontakt.name = document.getElementById('name').value;
kontakt.email = document.getElementById('email').value;
kontakt.plz = document.getElementById('plz').value;
kontakt.ort = document.getElementById('ort').value;
kontakt.strasse = document.getElementById('strasse').value;
try {
kontakt.pruefe();
}
catch(err) {
return window.alert(err.message);
}
kontakteSpeicher.neuerKontakt(kontakt);
$('#dialog').modal('hide');
}
It seems like you are closing your login function on the line before the call to close the modal.
Try to change this:
kontakteSpeicher.neuerKontakt(kontakt);}
$('#dialog').modal('hide');
to this:
kontakteSpeicher.neuerKontakt(kontakt);
$('#dialog').modal('hide'); }

JS validation mistakes

I got a problem with my validation of textboxes.
I am trying to get something like this.
Click on the button, give a message when something is not filled in, while you fill the empty textbox, the 1 message of that textbox should dissapear.
If you click the button again, with 1 empty textbox, only the message of that textbox should appear.
If you click it twice with nothing filled, it should only appear once ...
I messed something up here ...
can someone get me on again?
Thank you in advance!
var list2 = [];
function valideer(el) {
divOutput = document.getElementById("msgbox1");
var strValideer = "<ul>";
if (document.getElementById("naam").value === "") {
list2.push("naam");
} if (document.getElementById("voornaam").value === "") {
list2.push("voornaam");
} if (document.getElementById("straatnr").value === "") {
list2.push("straatnr");
} if (document.getElementById("postgem").selectedIndex === 0) {
list2.push("postgem");
} if (document.getElementById("telgsm").value === "") {
list2.push("telgsm");
} if (document.getElementById("email").value === "") {
list2.push("email");
}
for (var i = 0; i < list2.length; i++) {
strValideer += "<li><b>" + list2[i] + ": </b>verplicht veld</li>";
}
strValideer += "</ul>";
divOutput.innerHTML = strValideer;
}
function inputChange(el) {
divOutput = document.getElementById("msgbox1");
strValideer = "<ul>";
var naam = document.getElementById("naam").value;
if (naam !== "") {
list2.splice(list2.indexOf(el.name), 1);
}
for (var i = 0; i < list2.length; i++) {
strValideer += "<li><b>" + list2[i] + ": </b>verplicht veld</li>";
}
strValideer += "</ul>";
divOutput.innerHTML = strValideer;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="bootstrap/css/bootstrap_custom.min.css" type="text/css" rel="stylesheet" />
<!--lay-out met bootstrap grid-->
<link href="style.css" type="text/css" rel="stylesheet" />
<script src="woodfactory.js" type="text/javascript"></script>
</head>
<body>
<section class="container" id="userform">
<form action="js_form_ontvanger.html" method="get" class="form-horizontal" name="frmUserform" id="frmUserform" onsubmit="return validate(this)" oninput="inputChange(this)">
<fieldset>
<legend>Persoonlijke gegevens</legend>
<div class="container">
<div class="row">
<div class="span7">
<div class="control-group">
<label class="control-label" for="naam">naam:</label>
<div class="controls">
<input type="text" id="naam" name="naam" placeholder="naam" required onsubmit="valideer(this)" onclick="inputChange(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="voornaam">voornaam:</label>
<div class="controls">
<input type="text" id="voornaam" name="voornaam" placeholder="voornaam" required onsubmit="valideer(this)" onclick="inputChange(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="straatnr">straat+nr:</label>
<div class="controls">
<input type="text" id="straatnr" name="straatnr" placeholder="straat+nr" required onsubmit="valideer(this)" onclick="inputChange(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="postgem">post+gem:</label>
<div class="controls">
<select id="postgem" required onsubmit="valideer(this)" onclick="inputChange(this)">
<option value="">-- maak een keuze --</option>
<option value="2000">2000 Antwerpen</option>
<option value="9000">9000 Gent</option>
<option value="9300">9300 Aalst</option>
<option value="9400">9400 Ninove</option>
<option value="9450">9450 Haaltert</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="telgsm">tel/gsm:</label>
<div class="controls">
<input type="text" id="telgsm" name="telgsm" placeholder="tel/gsm" required onsubmit="valideer(this)" onclick="inputChange(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">email:</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="e-mail" required onsubmit="valideer(this)" onclick="inputChange(this)">
</div>
</div>
<!--einde span7-->
</div>
<div class="span4 valid">
<div id="msgbox1" class="alert alert-error">
<div class="span1">
</div>
</div>
</div>
<div class="span1">
<div class="span1">
</div>
<!--einde row-->
</div>
<!--einde container-->
</div>
</div>
</fieldset>
<fieldset>
<div class="container">
<div class="row">
<div class="span7">
<div class="control-group">
<div class="controls">
</div>
</div>
<div class="control-group">
<div class="controls">
</div>
</div>
<div class="control-group">
<div class="controls">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success" onclick="valideer(this)">Verzenden</button>
</div>
</div>
<div class="span4 valid">
<div id="msgbox3" class="alert alert-success"></div>
</div>
<div class="span1"><!--lege kolom--></div>
<!--einde row-->
</div>
<!--einde container-->
</div>
</fieldset>
</form>
</section>
<footer class="container">
<p>© 2013 The Wood Factory </p>
</footer>
</body>
</html>
I feel like your approach is a little more complicated than it needs to be.. See my attempt below
function valideer(current) {
var ids = ['naam', 'voornaam', 'straatnr', 'postgem', 'telgsm', 'email'];
var str = '<ul>';
ids.forEach(function(id) {
var el = document.getElementById(id);
if (el.value === '' && el !== current) {
str += "<li><b>" + id + ": </b>verplicht veld</li>";
}
});
str += '</ul>';
var outputDiv = document.getElementById("msgbox1");
outputDiv.innerHTML = str;
}
function handleFormSubmit(ev) {
ev.preventDefault();
valideer();
return false;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="bootstrap/css/bootstrap_custom.min.css" type="text/css" rel="stylesheet" />
<!--lay-out met bootstrap grid-->
<link href="style.css" type="text/css" rel="stylesheet" />
<script src="woodfactory.js" type="text/javascript"></script>
</head>
<body>
<section class="container" id="userform">
<form action="js_form_ontvanger.html" method="get" class="form-horizontal" name="frmUserform" id="frmUserform" onsubmit="handleFormSubmit">
<fieldset>
<legend>Persoonlijke gegevens</legend>
<div class="container">
<div class="row">
<div class="span7">
<div class="control-group">
<label class="control-label" for="naam">naam:</label>
<div class="controls">
<input type="text" id="naam" name="naam" placeholder="naam" required onclick="valideer(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="voornaam">voornaam:</label>
<div class="controls">
<input type="text" id="voornaam" name="voornaam" placeholder="voornaam" required onclick="valideer(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="straatnr">straat+nr:</label>
<div class="controls">
<input type="text" id="straatnr" name="straatnr" placeholder="straat+nr" required onclick="valideer(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="postgem">post+gem:</label>
<div class="controls">
<select id="postgem" required onclick="valideer(this)">
<option value="">-- maak een keuze --</option>
<option value="2000">2000 Antwerpen</option>
<option value="9000">9000 Gent</option>
<option value="9300">9300 Aalst</option>
<option value="9400">9400 Ninove</option>
<option value="9450">9450 Haaltert</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="telgsm">tel/gsm:</label>
<div class="controls">
<input type="text" id="telgsm" name="telgsm" placeholder="tel/gsm" required onclick="valideer(this)">
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">email:</label>
<div class="controls">
<input type="email" id="email" name="email" placeholder="e-mail" required onclick="valideer(this)">
</div>
</div>
<!--einde span7-->
</div>
<div class="span4 valid">
<div id="msgbox1" class="alert alert-error">
<div class="span1">
</div>
</div>
</div>
<div class="span1">
<div class="span1">
</div>
<!--einde row-->
</div>
<!--einde container-->
</div>
</div>
</fieldset>
<fieldset>
<div class="container">
<div class="row">
<div class="span7">
<div class="control-group">
<div class="controls">
</div>
</div>
<div class="control-group">
<div class="controls">
</div>
</div>
<div class="control-group">
<div class="controls">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success" onclick="valideer(this)">Verzenden</button>
</div>
</div>
<div class="span4 valid">
<div id="msgbox3" class="alert alert-success"></div>
</div>
<div class="span1">
<!--lege kolom-->
</div>
<!--einde row-->
</div>
<!--einde container-->
</div>
</fieldset>
</form>
</section>
<footer class="container">
<p>© 2013 The Wood Factory </p>
</footer>
</body>
</html>
Some notes and observations:
Since the only check being used for validation is if it contains, an empty string, I have generalised the valideer function to iterate and operate on a given array of ids. This can be made even better by querying for inputs on your form using a DOM query and handling it like that.
You have added a lot of inline event handlers in your dom.. Some of them are redundant and not really needed. For example, having a simgle onsubmit on your form element should suffice (no need to add it for every input inside the form)
You were using a global variable list2 this is what was causing the repeated entries each time you click the button. Localising the scope would fix that (by moving it inside a function)
You're already using required property, which means you no longer need a js function to validate if your inputs are empty or not.
You should also notice that list2 is a global variable, which means that you should clean it at the end of every valideer() invocation.
All in all, you code seems very verbose .. try to find a better way to express you needs.

php file not upload in ajax serialize

Here is my HTML
<form class="form form-vertical captcha_from" style="width: 50%;" method="post" action="<?php echo base_url('home/post_job_save'); ?>" enctype = "multipart/form-data">
<div class="container">
<div class="row form-group">
<div class="col-xs-12">
<ul class="nav nav-pills nav-justified thumbnail setup-panel">
<li class=""><a href="#step-1">
<h4 class="list-group-item-heading">Add Profile</h4>
<p class="list-group-item-text">First Step</p>
</a></li>
<li class=""><a href="#step-2">
<h4 class="list-group-item-heading">Add Resume</h4>
<p class="list-group-item-text">Second Step</p>
</a></li>
<li class=""><a href="#step-3">
<h4 class="list-group-item-heading">Add Cover Letter</h4>
<p class="list-group-item-text">Third Step</p>
</a></li>
<li class="active"><a href="#step-4">
<h4 class="list-group-item-heading">Add Photo</h4>
<p class="list-group-item-text">Fourth Step</p>
</a></li>
</ul>
</div>
</div>
<div class="row setup-content" id="step-1" >
<div class="col-xs-12">
<div class="col-md-12 well">
<h1>Add Profile</h1>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<div class="control-group">
<label>First Name*</label>
<div class="controls">
<input type="text" class="form-control" placeholder="First Name" name="first_name" required="">
</div>
</div>
<div class="control-group">
<label>Last Name*</label>
<div class="controls">
<input type="text" class="form-control" placeholder="last Name" name="last_name" required="">
</div>
</div>
<div class="control-group">
<label>Email*</label>
<div class="controls">
<input type="email" class="form-control" placeholder="Email" name="email" required="">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row setup-content" id="step-2">
<div class="col-xs-12">
<div class="col-md-12 well">
<h1 class="text-center">Add Resume</h1>
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label for="file">Select a file to upload</label>
<input type="file" name="resume">
<p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
</div>
</div>
</div>
</div><!-- /container -->
</div>
</div>
</div>
<div class="row setup-content" id="step-3">
<div class="col-xs-12">
<div class="col-md-12 well">
<h1 class="text-center">Add Cover Letter</h1>
<div class="form-group">
<label for="file">Select a file to upload</label>
<input type="file" name="cover_letter">
<p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
</div>
</div>
</div>
</div>
<div class="row setup-content" id="step-4">
<div class="col-xs-12">
<div class="col-md-12 well">
<h1 class="text-center">Add Photo</h1>
<div class="form-group">
<label for="file">Select a file to upload</label>
<input type="file" name="photo">
<p class="help-block">Only jpg,jpeg,png and gif file with maximum size of 1 MB is allowed.</p>
</div>
<input type="submit" id="apply_job_save_submit" class="btn btn-primary btn-md" value="Submit">
</div>
</div>
</div>
</div></form>
Js Code
$('#apply_job_save_submit').on('click', function(e) {
$.ajax({
type: 'post',
url: base_url+'jobs/home/apply_job_save',
data: $('#apply_job_save').serialize(),
success: function () {
document.getElementById('createarea').innerHTML = "SUCCESS";
}
});
});
php code:
if(isset($_FILES) && $_FILES["resume"]["name"]!=''){
$org_filename=$_FILES["resume"]["name"];
$arr=explode('.', $org_filename);
$filename='resume'.time().'.'.$arr[count($arr)-1];
$target_dir = UPLOAD_PATH;
$target_file = $target_dir . basename($filename);
move_uploaded_file($_FILES["resume"]["tmp_name"], $target_file);
$_POST['resume']=$filename;
}
if(isset($_FILES) && $_FILES["cover_letter"]["name"]!=''){
$org_filename=$_FILES["cover_letter"]["name"];
$arr=explode('.', $org_filename);
$filename='cover_letter'.time().'.'.$arr[count($arr)-1];
$target_dir = UPLOAD_PATH;
$target_file = $target_dir . basename($filename);
move_uploaded_file($_FILES["cover_letter"]["tmp_name"], $target_file);
$_POST['cover_letter']=$filename;
}
if(isset($_FILES) && $_FILES["photo"]["name"]!=''){
$org_filename=$_FILES["photo"]["name"];
$arr=explode('.', $org_filename);
$filename='photo'.time().'.'.$arr[count($arr)-1];
$target_dir = UPLOAD_PATH;
$target_file = $target_dir . basename($filename);
move_uploaded_file($_FILES["photo"]["tmp_name"], $target_file);
$_POST['photo']=$filename;
}
$this->general_model->insert('apply_job',$_POST);
Using this code I try to send some data into my database as well as upload files in my server using post method. All data posted fine but files are not uploaded.
Showing error while uploading files
Undefined index: resume
Undefined index: cover_letter
Undefined index: photo
how to resolve this
Undefined index: resume
Undefined index: cover_letter
Undefined index: photo
It means there are no such records in the $_FILES array, which in turn means that files were not uploaded. It happens because those files contents is not extracted upon form serialization. For solution refer this answer.

Posting Form Data to Redirect Page

I have a contact form that I'm using phpmailer to send out the information that was entered in the form and once sent is redirected to another page. Which works great, now what I would like is to have all the information that is sent to the email address also posted on the redirect page (ie - target.php). I use 3 pages 1 for html, 1 for verify(js) and 1 (phpmailer) to send. I have read other questions here and I know if you put the redirect page in the action"target.php" on the form you can get the data, except I have to put action"appt.php" which is my phpmailer form. I have tried:
<body>
<div class="container">
<div class="row">
<h2>Form data</h2>
<hr/>
<p>This is a simple page showing the data you have just submitted</p>
<?php
$fullnameField = $_POST['fullname'];
echo $fullnameField;
$addressField = $_POST['address'];
echo $addressField;
?>
</div>
</div>
</body>
I put the code above on the target.php page.
This is what I get when redirected to target.php page:
Notice: Undefined index: fullname in C:\xampp\htdocs\appt\target.php on line 14
Notice: Undefined index: address in C:\xampp\htdocs\appt\target.php on line 16
Also tried taking off Field at the end of the $ and still get the same code.
Here is html page:
<form id="defaultForm" method="post" class="form-horizontal" action="appt.php">
<fieldset>
<!-- Form Name -->
<legend>Appointment Request</legend>
<!-- Full Name -->
<div class="form-group">
<label class="col-md-4 control-label">Full Name*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input name="fullname" placeholder="Your Full Name" class="form-control" type="text">
</div>
</div>
</div>
<!-- Address -->
<div class="form-group">
<label class="col-md-4 control-label" >Address*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-road"></i></span>
<input name="address" placeholder="Your Address" class="form-control" type="text">
</div>
</div>
</div>
<!-- City -->
<div class="form-group">
<label class="col-md-4 control-label">City*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-home"></i></span>
<input name="city" placeholder="Your City" class="form-control" type="text">
</div>
</div>
</div>
<!-- State Select -->
<div class="form-group">
<label class="col-md-4 control-label">State*</label>
<div class="col-md-6 selectContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
<select name="state" class="form-control selectpicker" >
<option value=" " >Please select your state</option>
<option>Ohio</option>
<option>Pennsylvania</option>
</select>
</div>
</div>
</div>
<!-- Zip Code -->
<div class="form-group">
<label class="col-md-4 control-label">Zip Code*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-globe"></i></span>
<input name="zip" placeholder="Your Zip Code" class="form-control" type="text">
</div>
</div>
</div>
<!-- Phone Number -->
<div class="form-group">
<label class="col-md-4 control-label">Phone*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
<input name="phone" placeholder="(330)123-1234" class="form-control" type="text">
</div>
</div>
</div>
<!-- Email -->
<div class="form-group">
<label class="col-md-4 control-label">E-mail*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input name="email" placeholder="Your Email Address" class="form-control" type="text">
</div>
</div>
</div>
<!-- Appt Reason Select -->
<div class="form-group">
<label class="col-md-4 control-label">Appt Reason*</label>
<div class="col-md-6 selectContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
<select name="reason" class="form-control selectpicker" >
<option value=" ">Select Your Appointment Reason</option>
<option>Roofing</option>
<option>Siding</option>
<option>Doors</option>
<option>Windows</option>
<option>Decking</option>
<option>Comfort Rooms</option>
<option>Kitchen</option>
</select>
</div>
</div>
</div>
<!-- Appt Date -->
<div class="form-group">
<label class="col-md-4 control-label">Appt Date*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group date" id="datepicker"> <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
<input name="apptDate" placeholder="Date You Would Like Appt" class="form-control" type="text">
</div>
</div>
</div>
<!-- Appt Time-->
<div class="form-group">
<label class="col-md-4 control-label">Appt Time*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group date" id="timepicker"> <span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
<input name="apptTime" placeholder="Time You Would Like Appt" class="form-control" type="text">
</div>
</div>
</div>
<!-- Contact Method Select -->
<div class="form-group">
<label class="col-md-4 control-label">Contact You*</label>
<div class="col-md-6 selectContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-list"></i></span>
<select name="method" class="form-control selectpicker" >
<option value=" ">Select Your Contact Method</option>
<option>Email</option>
<option>Phone</option>
</select>
</div>
</div>
</div>
<!-- Project Description -->
<div class="form-group">
<label class="col-md-4 control-label">Project Description*</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group"> <span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" name="comment" placeholder="Project Description" rows="5"></textarea>
</div>
</div>
</div>
<!-- Captcha -->
<div class="form-group">
<label class="col-md-4 control-label">Captcha</label>
<div class="col-md-6 inputGroupContainer">
<div id="captchaContainer"></div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button type="submit" class="btn btn-primary" >Send <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</fieldset>
</form>
appt.php (phpmailer form):
<?php
session_start();
/**
* This example shows settings to use when sending via Google's Gmail servers.
*/
//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Etc/UTC');
//Contact Form Data
$fullnameField = $_POST['fullname'];
$addressField = $_POST['address'];
$cityField = $_POST['city'];
$stateField = $_POST['state'];
$zipcodeField = $_POST['zip'];
$phoneField = $_POST['phone'];
$emailField = $_POST['email'];
$apptReasonField = $_POST['reason'];
$apptDateField = $_POST['apptDate'];
$apptTimeField = $_POST['apptTime'];
$methodField = $_POST['method'];
$commentsField = $_POST['comment'];
require 'mailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer();
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.aol.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "username";
//Password to use for SMTP authentication
$mail->Password = "password";
//Set who the message is to be sent from
$mail->setFrom('sent from');
//Set who the message is to be sent to
$mail->addAddress('sent to');
//Set the subject line
$mail->Subject = 'Information For Appointment Wanted';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = <<<EOD
<br><p>Here is the information $fullnameField entered in your Appointment form.</p>
<br><hr><br>
<b>Name:</b> $fullnameField<hr><br>
<b>Address:</b> $addressField<hr><br>
<b>City:</b> $cityField<hr><br>
<b>State:</b> $stateField<hr><br>
<b>Zip Code:</b> $zipcodeField<hr><br>
<b>Phone #:</b> $phoneField<hr><br>
<b>E-mail:</b> $emailField<hr><br>
<b>Appointment Reason:</b> $apptReasonField<hr><br>
<b>Date Wanted For Appointment:</b> $apptDateField<hr><br>
<b>Time Wanted For Appointment:</b> $apptTimeField<hr><br>
<b>Best Way To Contact You:</b> $methodField<hr><br>
<b>Project Description:</b> $commentsField<hr><br>
EOD;
//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
$_SESSION[fullname]=$fullnameField;
$_SESSION[address]=$addressField;
$_SESSION[city]=$cityField;
$_SESSION[state]=$stateField;
$_SESSION[zip]=$zipcodeField;
$_SESSION[phone]=$phoneField;
$_SESSION[email]=$emailField;
$_SESSION[reason]=$apptReasonField;
$_SESSION[apptDate]=$apptDateField;
$_SESSION[apptTime]=$apptTimeField;
$_SESSION[method]=$methodField;
$_SESSION[comment]=$commentsField;
header('Location: http://localhost/appt/target.php');
}
Here is the target.php
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<title>Thank You</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container">
<div class="row">
<p class="thank">Thank you <b><?php echo $_SESSION['fullname']; ?></b> for scheduling an appointment with U.S. Pride Home Specialists. We will be contacting you by the contact method you chose as soon as possible.</p>
</div>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8">Your Appointment Form Information</div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Name:</b> <?php echo $_SESSION['fullname']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Address:</b> <?php echo $_SESSION['address']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>City:</b> <?php echo $_SESSION['city']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>State:</b> <?php echo $_SESSION['state']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Zip Code:</b> <?php echo $_SESSION['zip']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Phone:</b> <?php echo $_SESSION['phone']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>E-mail:</b> <?php echo $_SESSION['email']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Appointment Reason:</b> <?php echo $_SESSION['reason']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Appointment Date Wanted:</b> <?php echo $_SESSION['apptDate']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Appointment Time Wanted:</b> <?php echo $_SESSION['apptTime']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Way You Wanted Contacted:</b> <?php echo $_SESSION['method']; ?></div><div class="col-md-2"></div>
</div>
<hr>
<div class="row">
<div class="col-md-2"></div><div class="col-md-8"><b>Project Description:</b> <?php echo $_SESSION['comment']; ?></div><div class="col-md-2"></div>
</div>
</div>
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
</body>
</html>
You need to store your form data in a database table or session for persistence.
something like:
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}else{
// BUILD $form_data here
$_SESSION['temp_form_data'] = serialize($form_data);
and after redirect just get data from session.
header('Location: http://localhost/appt/target.php');
}
And in the page that you show after redirect
in target.php
$form_data = unserialize($_SESSION['temp_form_data']);
// Use form data here
Another option would be to store it in database and retrieve it after redirect.
I do not suggest placing form data params and values over a url.

Updating a div via Modal form using AJAX

I am having trouble updating a specific div on a form using AJAX. I'm using a modal form that allows the user to edit existing information and save their changes. Once the user clicks on a 'save' button, the modal should close and update the text on the form from which it was called.
Currently the changes are saved into my DB but the modal redirects with the HTML code fragment that contains the new information to http://localhost:56544/client/UpdateClientDetails.
I want to run the UpdateClientDetails action but stay on the current view (Index.cshtml).
I have been referring to Apress's Pro ASP.NET MVC 5 so far since I am new to these technologies.
ClientController.cs
public ActionResult EditClientDetails(int id)
{
Client temp_client = _clientService.GetClientInfo(id);
ViewBag.ProvinceList = PopulateProvinceList(temp_client.Province);
return PartialView(temp_client);
}
public ActionResult UpdateClientDetails(Client newClientDetails)
{
_clientService.UpdateClient(newClientDetails);
return PartialView(newClientDetails);
}
Index.cshtml View - desired div shown only
<div class="panel-body" id="client-#id-info">
<label>Client Id: #client.Id</label>
<address>
#client.Address1, #client.Address2<br>
#client.City, #client.Province<br>
#client.PostalCode<br>
<abbr title="Phone">P:</abbr> #client.PhoneNumber
</address>
<div><button value="#id" type="button" class="btn btn-primary btn-update-client">Change</button></div>
</div>
UpdateClientDetails.cshtml View
#using ProductManager.Models
#model Client
#Model.Address1, #Model.Address2<br>
#Model.City, #Model.Province<br>
#Model.PostalCode<br>
<abbr title="Phone">P:</abbr> #Model.PhoneNumber
EditClientDetails.cshtml
#model Client
#using ProductManager.Models
#{
var attributes = new Dictionary<string, object>();
attributes.Add("class", "form-horizontal");
AjaxOptions ajaxOpts = new AjaxOptions
{
UpdateTargetId = "client-" + #Model.Id + "-info",
Url = Url.Action("UpdateClientDetails")
};
}
<div class="modal-header" id="EditClientDetails">
<h4 class="modal-title" id="exampleModalLabel">#Model.Name - ID: #Model.Id</h4>
</div>
#using (Ajax.BeginForm(null, null, ajaxOpts, attributes))
{
<div class="modal-body">
<div class="form-group">
<label for="clientAddress1" class="col-xs-3 control-label">Address 1</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="clientAddress1-#Model.Id" name="Address1" value="#Model.Address1">
</div>
</div>
<div class="form-group">
<label for="clientAddress2" class="col-xs-3 control-label">Address 2</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="clientAddress2-#Model.Id" name="Address2" value="#Model.Address2">
</div>
</div>
<div class="form-group">
<label for="clientCity" class="col-xs-3 control-label">City</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="clientCity-#Model.Id" name="City" value="#Model.City">
</div>
</div>
<div class="form-group">
<label for="clientProvince" class="col-xs-3 control-label">Province</label>
<div class="col-xs-9">
#Html.DropDownListFor(model => model.Province, (IEnumerable<SelectListItem>)ViewBag.ProvinceList, new { #class = "form-control", #id = "clientProvince-" + #Model.Id })
</div>
</div>
<div class="form-group">
<label for="clientPostalCode" class="col-xs-3 control-label">Postal Code</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="clientPostalCode-#Model.Id" name="PostalCode" value="#Model.PostalCode">
</div>
</div>
<div class="form-group">
<label for="clientPhoneNumber" class="col-xs-3 control-label">Phone #</label>
<div class="col-xs-9">
<input type="text" class="form-control" id="clientPhoneNumber-#Model.Id" name="PhoneNumber" value="#Model.PhoneNumber">
</div>
</div>
<div>
<input type="hidden" id="clientName-#Model.Id" name="Name" value="#Model.Name">
</div>
<div>
<input type="hidden" id="clientID-#Model.Id" name="Id" value="#Model.Id">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
}

Categories