When I try and submit a form here (php page) - http://portsdownhb.ogdigital.co.uk/ the page throws the error 'Failed to load resource: the server responded with a status of 404 (Not Found)' when pressing submit, despite the exact same working on a HTML page.
The error suggests the page http://portsdownhb.ogdigital.co.uk/contact-us/contact.php is trying to be found but it should be http://portsdownhb.ogdigital.co.uk/contact.php
$(function () {
$('#contact-form').validator();
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="First Name" required="required" data-error="Name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Email" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-error="Please, leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<div class="g-recaptcha" data-sitekey="****"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<input type="submit" class="btn btn-success btn-send" value="Send message">
</div>
</div>
</div>
</form>
Related
I'm working on a site and the goal was to submit a contact form without reloading the whole page. I tried different paths, but nothing seems to help. It looks like the "on submit" part doesn't work at all I'm still leaving site to contact_form.php. I would be very thankful for any help, thanks in advance
This is js part:
$("#contact-form").on('submit', function(){
var name = $("#form_name").val();
var lastname = $("#form_lastname").val();
var email = $("#form_email").val();
var subject = $("#form_subject").val();
var message = $("#form_message").val();
var dataString = 'name=' + name + '&lastname=' + lastname + '&email=' + email +'&subject'+subject + '&message=' + message;
$.ajax({
type: "POST",
url: "contact_form.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
return false;
});
and here html:
<form id="contact-form" method="post" action="contact_form.php" role="form">
<div class="messages"></div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_name" type="text" name="name" class="form-control" placeholder="Name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class ="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_lastname" type="text" name="surname" class="form-control" placeholder="Last name" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_email" type="text" name="mail" class="form-control" placeholder="Email" required="required" data-invalid-message="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Subject" required="required" data-invalid-message="Please enter your name">
</div>
</div>
</div>
<div class="form-row">
<div class="col-md-12">
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message" rows="4" required="required" data-invalid-message="Please enter your name"></textarea>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success" value="Send message">
</div>
</div>
</form>
You can start by passing the event and stopping the default behaviour, which would be to hit the action of the form.
$("#contact-form").on('submit', function(e){
e.preventDefault();
// rest of code... ( do not return true in the end of this handler though )
When im doing a ajax call to the update.php from profiles.php it go a blank page update.php but it does make the changes if i go back to the profile.php
idk what im doing wrong what im missing??
profile.php
<?php include_once("includes/config.php");
if (isset($_GET['id']))
{
$Nome = $_GET['id'];
$sql = "SELECT * FROM utilizadores WHERE id='$Nome'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
if($row = $result->fetch_assoc()) {
echo'
<br>
<form action="update.php" method="post" id="go" class="go" accept-charset="utf-8">
<div class="content" >
<div class="container-fluid">
div class="row">
<div class="col-md-8">
<div class="card">
<div class="header">
<h4 class="title">Perfil do paciente</h4>
</div>
<div class="content">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Nome</label>
<input type="text" class="form-control" disabled placeholder="Nome" value="'.$row["Nome"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Apelido</label>
input type="text" class="form-control" disabled placeholder="Apelido" value="'.$row["Apelido"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="exampleInputEmail1">Email</label>
<input type="email" class="form-control" name="Email "disabled placeholder="Email" value="'.$row["Email"].'">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Idade</label>
<input type="text" class="form-control" disabled placeholder="Idade" value="'.$row["Idade"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Sexo</label>
<input type="text" class="form-control" disabled placeholder="Sexo" value="'.$row["Sexo"].'">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Telefone</label>
<input type="text" class="form-control" disabled placeholder="Telefone" value="'.$row["Telefone"].'">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Peso</label>
<input type="text" class="form-control" disabled placeholder="Peso" value="'.$row["Peso"].'Kg">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Altura</label>
<input type="text" class="form-control" disabled placeholder="Altura" value="'.$row["Altura"].'Cm">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Perimetro Abdominal</label>
<input type="text" class="form-control" disabled placeholder="Perimetro abdominal" value="'.$row["Perimetro_abdominal"].'Cm">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label>Perimetro Anca </label>
<input type="text" class="form-control" disabled placeholder="Perimetro Anca" value="'.$row["Perimetro_anca"].'Cm">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Medicação:</label>
<textarea rows="3" class="form-control" name="Medicacao" placeholder="Medicação" id="Medicacao">'.$row["Medicacao"].'</textarea>
</div>
<input type=hidden name=hidden value="'.$row["id"].' ">
<input type="submit" name="med" class="btn btn-info btn-fill pull-right" value="Atualizar" id="med"></div>
<div class="col-md-6">
<div class="form-group">
<label>Observações:</label>
<textarea rows="3" name="Obs" class="form-control" placeholder="Anotar observações" id="Obs">'.$row["Obs"].' </textarea>
</div>
<input type="submit" name="obs" class="btn btn-info btn-fill pull-right" value="Atualizar">
</div>
</div></form>';
}
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_close($conn);
?>
<script type="text/javascript">
$( "#med" ).click(function() {
$.ajax({
type: "POST",
url: "update.php",
success: function(){
alertify.success('campo atualizado com sucesso');
}
});
});
</script>
update.php
include_once("includes/config.php");
if(isset($_POST['med'])) {
$UpdateQuery = "UPDATE utilizadores SET Medicacao='$_POST[Medicacao]' WHERE id='$_POST[hidden]' ";
$res = mysqli_query($conn,$UpdateQuery);
if($res == 1)
{
//header('Location: ' . $_SERVER['HTTP_REFERER'],true, 301 );
}
?>
You are not cancelling the default submit event of your submit button when you handle the click event. That causes the form to be submitted in the normal way as well by going to the url that is set in the action attribute of the form.
Apart from that, you also need to add the data to the ajax request.
This:
$( "#med" ).click(function() {
$.ajax({
type: "POST",
url: "update.php",
success: function(){
alertify.success('campo atualizado com sucesso');
}
});
});
should be:
$( "#med" ).click(function(event) {
// cancel default submit event
event.preventDefault();
$.ajax({
type: "POST",
// add data
data: $('form#go').serialize(),
url: "update.php",
success: function(){
// Do whatever you need to here
console.log('campo atualizado com sucesso');
}
});
});
Webdesigner seeking deeper understanding of forms...
Hello!
I have two similar contact forms on the same page which are sent out through ajax.
Each contact form is working fine on its own, but as soon as I put them both together on the page, one of them stops working...
I know there are issues having two forms on one page, so I gave them different ids (#contactForm1, #contactForm2), which is working, but I am sure there has to be a smarter way to do this...
What advice would you give me to make this working the best way?
My html as followed:
<form role="form" id="contactForm" class="contact-form" data-toggle="validator">
<section id="content">
<!-- NAME -->
<div class="form-group">
<div class="controls">
<input autocomplete="off" type="text" id="name" class="form-control" placeholder="Your Name" required data-error="Please enter your name">
<div class="help-block with-errors"></div>
</div>
</div>
<!-- SUBJECT -->
<div class="form-group">
<div class="controls">
<input autocomplete="off" type="text" id="msg_subject" class="form-control" placeholder="Company Name" required data-error="Please enter your message subject">
<div class="help-block with-errors"></div>
</div>
</div>
<!-- MESSAGE -->
<div class="form-group">
<div class="controls">
<textarea id="message" rows="7" placeholder="short description" class="form-control" required data-error="Write your message"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<!-- EMAIL -->
<div class="form-group">
<div class="controls">
<input autocomplete="off" type="email" class="email form-control" id="email" placeholder="Email Adress" required data-error="Please enter your email">
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" id="submit" class="btn btn-success"></i> SEND</button>
<div id="msgSubmit" class="h3 text-center hidden"></div>
<div class="clearfix"></div>
</section>
</form>
Here my JS:
$("#contactForm").validator().on("submit", function (event) {
if (event.isDefaultPrevented()) {
formError();
submitMSG(false, "got everything right?");
} else {
event.preventDefault();
submitForm();
}
});
function submitForm(){
var name = $("#name").val();
var email = $("#email").val();
var msg_subject = $("#msg_subject").val();
var message = $("#message").val();
$.ajax({
type: "POST",
url: "assets/php/form-process.php",
data: "name=" + name + "&email=" + email + "&msg_subject=" + msg_subject + "&message=" + message,
success : function(text){
if (text == "success"){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
Use class="contactForm" instead of id="contactForm" and in js:
(".contactForm").validator().on("submit", function (event) {...}
i'm have confusing problem about uploading image in multiple input text like registration page. i found solution but the image is not save to database or folder.
please help me all...
my admin.php view
<script>
//upload img
$('#form').ajaxForm({
//uploadimg is my form id
dataType: 'json',
success: processJson
});
function processJson(data) {
if(data.msg=="success"){
alert('Upload sukses.');
}
else{
alert('Upload gagal.');
}
}
function add_edulibs()
{
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Tambah Data'); // Set Title to Bootstrap modal title
}
function edit_edulibs(id)
{
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url : "<?php echo site_url('edulibs/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('[name="id"]').val(data.id);
$('[name="nama"]').val(data.nama);
$('[name="nim"]').val(data.nim);
$('[name="pembimbing"]').val(data.pembimbing);
$('[name="subyek"]').val(data.subyek);
$('[name="judul"]').val(data.judul);
$('[name="tanggal"]').datepicker('update',data.tanggal);
$('[name="alamat"]').val(data.alamat);
$('[name="kontak"]').val(data.kontak);
$('[name="email"]').val(data.email);
$('[name="penerbit"]').val(data.penerbit);
$('[name="file_upload"]').val(data.gambar);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Data'); // Set title to Bootstrap modal title
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error mendapatkan data');
}
});
}
</script>
<div class="modal fade" id="modal_form" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title">Kelola Data</h3>
</div>
<div class="modal-body form">
<form action="<?php echo base_url('edulibs/do_upload/')?>" method="post" enctype="multipart/form-data" id="form" class="form-horizontal">
<input type="hidden" value="" name="id"/>
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-3">Nama</label>
<div class="col-md-9">
<input name="nama" placeholder="Nama Lengkap" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">NIM</label>
<div class="col-md-9">
<input name="nim" placeholder="Nomor Induk Mahasiswa" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Pembimbing</label>
<div class="col-md-9">
<input name="pembimbing" placeholder="Nama Pembimbing" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Subyek</label>
<div class="col-md-9">
<input name="subyek" placeholder="Subyek" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Judul</label>
<div class="col-md-9">
<textarea name="judul" placeholder="Judul Karya Tulis" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Tanggal Terbit</label>
<div class="col-md-9">
<input name="tanggal" placeholder="Tahun-Bulan-Hari" class="form-control datepicker" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Alamat</label>
<div class="col-md-9">
<textarea name="alamat" placeholder="Alamat Lengkap" class="form-control"></textarea>
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Kontak</label>
<div class="col-md-9">
<input name="kontak" placeholder="Nomor Kontak" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Email</label>
<div class="col-md-9">
<input name="email" placeholder="Alamat e-mail" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Penerbit</label>
<div class="col-md-9">
<input name="penerbit" placeholder="Nama Penerbit" class="form-control" type="text">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3">Foto</label>
<div class="col-md-9">
<input type="file" name="file_upload" />
<span class="help-block"></span>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save()" class="btn btn-primary">Simpan</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Batalkan</button>
</div>
My edulibs.php controller
public function admin()
{
$this->load->view('admin');
}
function do_upload(){
$config['upload_path'] = "./uploads/foto/";
$config['allowed_types'] = 'gif|jpg|png|JPEG';
$config['file_name'] = url_title($this->input->post('file_upload'));
$this->upload->initialize($config);
if(!$this->upload->do_upload('file_upload'))
{
echo $this->upload->display_errors();
}
else{
$data = array(
'gambar'=>$this->upload->file_name
);
$this->edulib_model->insert($data,'edulib');
}
}
My edulib_model.php
public function save($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
public function insert($data,$table){
$this->db->insert($data,$table);
}
I am using Tinymce for a text editor to edit some text before submitting the form. I then use jQuery to submit the form without loading the page, but when I submit the form it takes two clicks to submit the value of the textarea..
Here is my code for my form:
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Write News Release</h1>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<span id="result"></span>
<div class="panel panel-default">
<div class="panel-heading">News release form</div>
<div class="panel-body">
<form id="myForm" action="dataentry/save_data.php" method="post">
<div class="col-lg-6">
<div class="form-group">
<label>Story date</label>
<div class="controls">
<div class="input-group">
<input type="text" name="date_story" id="date_story" class="datepicker form-control" placeholder="choose date">
<label for="date-picker-2" class="input-group-addon btn"><span class="glyphicon glyphicon-calendar"></span></label>
</div>
</div>
</div>
<div class="form-group">
<label>Writer/Author</label>
<input type="text" name="writer" id="writer" class="form-control" placeholder="Enter Name">
</div>
<div class="form-group">
<label>Headline</label>
<input type="text" name="headline" id="headline" class="form-control" placeholder="Enter headline">
</div>
<div class="form-group">
<label>Source</label>
<input type="text" name="source" id="source" class="form-control" placeholder="Enter source">
</div>
</div>
<div class="col-lg-10">
<div class="form-group">
<label>Story</label>
<textarea name="story" id="story" class="form-control" rows="10"></textarea>
</div>
<div class="form-group">
<label>Keywords</label>
<input type="text" name="keywords" id="keywords" class="form-control" placeholder="Enter keywords separated by comma(,)">
</div>
<input type="hidden" name="save" value="nr">
<button id="submit" name="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="../js/save_data_admin.js"></script>
<script src="../js/jquery-1.11.2.js"></script>
<script src="//tinymce.cachefly.net/4.1/tinymce.min.js"></script>
<script>tinymce.init({selector:'textarea'});</script>
and this is inside of save_data_admin.js:
I alredy use tinyMCE.triggerSave() and other getContent but when I try the button isn't submit the form.
$("#submit").click(function() {
//tinyMCE.triggerSave();
$.post( $("#myForm").attr("action"), $("#myForm :input").serializeArray(), function(save){ $("#result").html(save); } );
clearInput();
});
$("#myForm").submit(function(){
return false;
});
function clearInput(){
$('input[type="text"], input[type="date"], textarea').val('');
}
and save_data.php:
<?php
session_start();
include_once '../../include/CsppoAdmin.php';
$csppoAdmin = new CsppoAdmin();
//echo $_POST['date_story']." ".$_POST['writer']." ".$_POST['headline']." ".$_POST['source']." ".$_POST['story']." ".$_POST['keywords'];
if ($_REQUEST['save']=='nr')
{
if (!empty($_REQUEST['date_story']) && !empty($_REQUEST['writer']) && !empty($_REQUEST['headline']) && !empty($_REQUEST['source']) &&!empty($_REQUEST['story']) && !empty($_REQUEST['keywords'])) {
extract($_REQUEST);
$register = $csppoAdmin->add_news_release($date_story,$writer,$headline,$source,$story,$keywords);
if ($register)
echo '<p style="color:green">News release was successful saved!</p>';
else
echo '<p style="color:red">Failed. Inputted already exits please try again.</p>';
}else
echo '<p style="color:red">Fill up all fields</p>';
}
?>
Maby something like this:
$("#myForm").submit(function(){
tinyMCE.triggerSave(); //must called. fills the hidden textarea
//format some data maby?
//post the data
$.post( $("#myForm").attr("action"), $("#myForm :input").serializeArray(), function(save){ $("#result").html(save); } );
return false;
});