I got some AJAX code, but it doesn't get response from server. I tried many things but can't figure out why this is not working. I am running on a localhost setup with XAMPP. I want the echo to be seen in the span with id status.
AJAX on index.php
$(document).ready(function() {
$("#registrationform").submit(function() {
$.ajax({
type: "POST",
url: "registration.php",
data: $("#registrationform").serialize(),
success: function(data) {
$("#status").html(data);
}
});
alert("Ajax not running")
});
});
FORM on index.php
<form id="registrationform" action="">
Voornaam: <input type="text" name="voornaam" /><br />
Tussenvoegsel: <input type="text" name="tussenvoegsel" /><br />
Achternaam: <input type="text" name="achternaam" /><br />
Geslacht: <?php include_once("parser/geslacht.php"); ?><br />
Land: <?php include_once("parser/land.php"); ?><br />
Geboortedatum: <?php include_once("parser/gebdatum.php"); ?><br />
E-mail: <input type="text" name="e-mail" /><br />
Wachtwoord: <input type="password" name="password" /><br />
Hertype wachtwoord: <input type="password" name="password2" /><br /><br />
De onderstaande vraag is nodig om uw wachtwoord terug te halen mocht u die vergeten zijn.<br />
Geheime vraag: <select name="geheimevraag" id="geheimevraag">
<option value="huisdier">Wat is de naam van jouw eerste huisdier?</option>
<option value="moedername">Wat is de meisjesnaam van je moeder?</option>
<option value="eerstebaas">Hoe heet je eerste baas?</option>
<option value="eigenvraag">Eigen vraag opstellen</option>
</select><br/>
<span id="anders" style="display:none;">Eigen vraag: <input type="text" name="eigen_vraag" style="width:300px;"/></span><br />
Antwoord: <input type="password" name="gantwoord" /><br />
</form>
<input type="submit" value="Submit"/>
<span id="status"></span>
registration.php
if(!empty($_POST)){
echo "There is a value";
}else{
echo "enter a value";
}
?>
write the <input type="submit" value="Submit"/> before </form> close tag
<form id="registrationform" action="">
Voornaam: <input type="text" name="voornaam" /><br />
Tussenvoegsel: <input type="text" name="tussenvoegsel" /><br />
Achternaam: <input type="text" name="achternaam" /><br />
Geslacht: <?php //include_once("parser/geslacht.php"); ?><br />
Land: <?php //include_once("parser/land.php"); ?><br />
Geboortedatum: <?php //include_once("parser/gebdatum.php"); ?><br />
E-mail: <input type="text" name="e-mail" /><br />
Wachtwoord: <input type="password" name="password" /><br />
Hertype wachtwoord: <input type="password" name="password2" /><br /><br />
De onderstaande vraag is nodig om uw wachtwoord terug te halen mocht u die vergeten zijn.<br />
Geheime vraag: <select name="geheimevraag" id="geheimevraag">
<option value="huisdier">Wat is de naam van jouw eerste huisdier?</option>
<option value="moedername">Wat is de meisjesnaam van je moeder?</option>
<option value="eerstebaas">Hoe heet je eerste baas?</option>
<option value="eigenvraag">Eigen vraag opstellen</option>
</select><br/>
<span id="anders" style="display:none;">Eigen vraag: <input type="text" name="eigen_vraag" style="width:300px;"/></span><br />
Antwoord: <input type="password" name="gantwoord" /><br />
<input type="submit" value="Submit"/>
</form>
<span id="status"></span>
Your jquery script script:
<script>
$(document).ready(function() {
$("#registrationform").submit(function(ev){
ev.preventDefault();
$.ajax ({
type: "POST",
url: "registration.php",
data: $("#registrationform").serialize(),
success: function(data) {
$("#status").html(data);
}
});
alert("Ajax not running")
});
});
</script>
Please try this code
$(document).ready(function() {
$("#registrationform").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "registration.php",
data: $("#registrationform").serialize(),
success: function(data) {
$("#status").html(data);
}
});
alert("Ajax not running")
});
});
You have to prevent the default behavior of the submit button
$("#registrationform").submit(function(ev) {
ev.preventDefault();
...
Then you need to implement a proper failure handler, your current one will allways alert because that ajax is async
$.ajax({
type: "POST",
url: "registration.php",
data: $("#registrationform").serialize(),
success: function(data) {
console.log(data); // check your console to see if request is success and what it contains
$("#status").html(data);
}
}).fail(function () {
alert("oh noes");
});
You need to prevent the default behavior of the form so that the page won't change location, and so that JS can do the AJAX request.
$("#registrationform").submit(function(e) {
e.preventDefault(); //this one
$.ajax({
type: "POST",
url: "registration.php",
data: $("#registrationform").serialize(),
success: function(data) {
$("#status").html(data);
}
});
});
Related
I am showing radio button in html
<body ng-app="myApp" ng-controller="MainCtrl">
<form enctype="multipart/form-data" id="FileUploadForm" name="FileUpload" ng-submit="submitData(FileUpload)" novalidate>
<div style="text-align: left;font-size: 20px;padding: 10px;color: #487eaa;">
<font> Upload Resume</font>
</div>
<div class="file-upload">
<input type="text" id="name" name="name" ng-model="name" />
<br /><br />
<input type="text" id="place" name="place" ng-model="place" />
<br /><br />
<label class="control-label" for="Dilation" style="padding:3px 0;">Dilation</label>
<label>Yes <input type="radio" name="dilation" id="dilation" ng-model="dilation" value="Yes" ng-checked="false" /></label>
<label>No <input type="radio" name="dilation" id="dilation" ng-model="dilation" value="No" ng-checked="false" /></label>
<br /><br />
<label class="control-label" for="NCT" style="padding:3px 0;">NCT</label>
<label>Right <input type="radio" name="nct" id="nct" ng-model="nct" value="Right" ng-checked="false" /></label>
<label>Left <input type="radio" name="nct" id="nct" ng-model="nct" value="Left" ng-checked="false" /></label>
<br /><br />
<input type="file" id="i_file" name="file" ng-file-select="onFileSelect($files)" multiple />
<br /><br />
<input type="submit" id="submit" name="submit" value="submit" ng-disabled="FileUploadForm.$invalid" />
<br /><br />
<div ng-bind="dilation" ng-hide="dilation"></div>
<div ng-bind="nct" ng-hide="nct"></div>
</div>
</form>
</body>
And here is my angular controller
angular.module('myApp', ['angularFileUpload'])
.controller('MainCtrl', function($scope, $http, $upload) {
$scope.onFileSelect = function($files) {
$scope.message = "";
for (var i = 0; i < $files.length; i++) {
var file = $files[i];
console.log(file);
$scope.upload = $upload.upload({
url: 'upload.php',
method: 'POST',
data: {name: $scope.name,place: $scope.place, dilation: dilation, nct: nct },
file: file
}).success(function(data, status, headers, config) {
$scope.message = data;
}).error(function(data, status) {
$scope.message = data;
});
}
};
});
And here is my php code(upload.php)
$result = array("filename" => $_FILES["file"],"name" => $_POST['name'],"place" => $_POST['place'],"dilation" => $_POST['dilation'],"nct" => $_POST['nct']);
echo json_encode($result);
Here is a fiddle : http://jsfiddle.net/aabyv4kx/
and getting response like this
name:qqq,place:eeee,dilation:{\"ng339\":8},nct:{\"ng339\":10}
I am new to angularjs please help me sort this.
I have struggled for quite some time now with this but can figure out whats wrong.
I have an index file where I'm loading the contend with ajax (in this case a form stored in add_admin.php). I have a form which loads just perfectly in a div after clicking a menu item(calling the ajax function - this part works). But if I want to submit that form with jQuery afterwords using the
$(".loginform").submit(function(e) {});
it doesn't get called. I'm suspecting the reason is that the form was not present on the page at the time it was loaded. If I move the form directly to the index page, the function works perfectly.
$(document).ready(function () {
$(".loginform").submit(function(e) {
data = $("#loginform").serialize();
password = document.getElementById("user_pass").value;
passtosubmit=hex_sha512(password);
data += "&pass=" + encodeURIComponent(passtosubmit);
password="";
//$.post("modules/process/process_add_admin.php", data);
//alert(data);return false;
$.ajax({
type: "POST",
url: "/modules/process/process_add_admin.php",
data: data,
success: function() {
$('#main_panel_container').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png'/>");
});
}
});
return false;
});
});
The form to submit
add_admin.php
<div id="contact_form">
<form name="loginform" class="loginform" id="loginform" action="#" method="post">
<label><strong>Username</strong></label><input type="text" name="username" id="user_login" size="28" class="input" />
<br />
<label><strong>Password</strong></label><input type="password" name="p" id="user_pass" size="28" class="input"/>
<br />
<label><strong>E-mail</strong></label><input type="text" name="email" id="user_email" size="28" class="email" />
<br />
<label><strong>First Name</strong></label><input type="text" name="fname" id="user_fname" size="28" class="input" />
<br />
<label><strong>Last Name</strong></label><input type="text" name="lname" id="user_lname" size="28" class="input" />
<br />
<input id="save" class="addbutton" type="submit" value="Add" onclick=""/>
</form>
</div>
can anyone please advise?
Thanks
Here's what I normally do, add a onSubmit event in the form tag
<form name="loginform" class="loginform" id="loginform" action="#" method="post" onSubmit="return addContent('loginform');">
and then with javascript
function addContent(frm) {
//anything you wanna do before you post
$.post(
url,
$('#' + frm).serialize(),
function (data) {
result = data;
}
)
.success(function() {
//add your success proccesses
})
.complete(function() {
})
.error(function() {
alert('An error has occurred.');
});
return false;// this stops the form from actually posting
}
Use this code:
<div id="contact_form">
<form name="loginform" class="loginform" id="loginform" action="javascript:add_admin();" method="post">
<label><strong>Username</strong></label><input type="text" name="username" id="user_login" size="28" class="input" />
<br />
<label><strong>Password</strong></label><input type="password" name="p" id="user_pass" size="28" class="input"/>
<br />
<label><strong>E-mail</strong></label><input type="text" name="email" id="user_email" size="28" class="email" />
<br />
<label><strong>First Name</strong></label><input type="text" name="fname" id="user_fname" size="28" class="input" />
<br />
<label><strong>Last Name</strong></label><input type="text" name="lname" id="user_lname" size="28" class="input" />
<br />
<input id="save" class="addbutton" type="submit" value="Add"/>
</form>
</div>
You have to define a function on javascript use this javascript code
function add_admin(){
data = $("#loginform").serialize();
password = document.getElementById("user_pass").value;
passtosubmit=hex_sha512(password);
data += "&pass=" + encodeURIComponent(passtosubmit);
password="";
//$.post("modules/process/process_add_admin.php", data);
//alert(data);return false;
$.ajax({
type: "POST",
url: "/modules/process/process_add_admin.php",
data: data,
success: function() {
$('#main_panel_container').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
}
Thanks!
I was having the same issue when I ran across this thread. This solution did not quite work for what I needed to do, but what I wound up doing is just calling my page initiate function again after I loaded the AJAX form. This added the new AJAX form to the event calls and it worked perfectly.
I'm having a problem with my Jquery register validation.
I've a problem getting the error message back if you don't fill in a name.
jQuery(document).ready(function () {
$('#register').submit(function () {
var action = $(this).attr('action');
var error = '<div class=\"states\"><li class=\"warning\"><strong>Fout!</strong><br /> U hebt geen naam opgegeven.</li> <br /></ul>';
$(".states li").slideUp(750, function () {
$('.states li').hide();
$('#submit')
.after('<img src="images/ajax-loader.gif" class="loader" />')
.attr('disabled', 'disabled');
$.post(action, {
name: $('#name').val(),
realname: $('#realnam').val(),
pass: $('#pass').val(),
repeatpass: $('#repeatpass').val(),
mail: $('#mail').val(),
mailadres: $('#mailadres').val(),
});
if (name == "") {
$(error).slideDown('slow');
}
});
return false;
});
});
And my HTML code:
<script src="js/aanmelden.js"></script>
<?php
include_once 'include/config.php';
?>
<div class="text-section">
<h1>Aanmelden</h1>
<p>Hier kunt u zich <strong>gratis</strong> aanmelden op <?php echo $sNaam; ?></p>
</div>
<div class="states">
<li class="warning"><strong>Waarschuwing</strong> Deze pagina is (nog) niet af, hier wordt aangewerkt.</li> <br />
</ul>
<form method="POST" action="bin/register.php" id="register">
<fieldset>
<legend>Naam</legend>
<label id="username">Gebruikersnaam:</label>
<input type="text" class="text" name="gebruikersnaam" id="name" placeholder="Uw gebruikersnaam" /> <br />
<label id="realname">Uw echte naam:</label>
<input type="text" class="text" name="echtenaam" id="realnam" placeholder="Uw echte naam" /> <br />
</fieldset>
<fieldset>
<legend>Wachtwoord</legend>
<label id="password">Uw wachtwoord:</label>
<input type="password" class="text" id="pass" name="wachtwoord" placeholder="Uw wachtwoord" /> <br />
<label id="repeatpassword">Uw wachtwoord nogmaals:</label>
<input type="password" class="text" id="repeatpass" name="hwachtwoord" placeholder="Uw wachtwoord nogmaals" /> <br />
</fieldset>
<fieldset>
<legend>Mail</legend>
<label id="mailadres">Uw mail adres:</label>
<input type="text" class="text" name="mail" id="mail" placeholder="Uw mail adres" /> <br />
<label id="repeatmail">Uw mail adres nogmaals:</label>
<input type="text" class="text" name="hmail" id="mailadres" placeholder="Uw mail adres nogmaals" /> <br />``
</fieldset>
<input type="submit" name="submit" value="Registreren" class="orange" id="submit" />
<br />
</form>
My problem(demo at http://mijnrpg.eu and then the second tab). If you click on the button where it says "Registreren", you will see what I mean. It isn't giving an error.
$.post(action, {
name: $('#name').val(),
realname: $('#realnam').val(),
pass: $('#pass').val(),
repeatpass: $('#repeatpass').val(),
mail: $('#mail').val(),
mailadres: $('#mailadres').val(),
});
use serialize
$('#register').serialize();
your post function would be
$.post(action,
$('#register').serialize(),
function(resp)
{
///
}
);
And to answer your question try
$('#name').val()==''
Here i slightly modified your code http://jsfiddle.net/eVA8s/10/
So basically what you want to do is create div tag with id error-message and set it style to hidden,
second watch at your tags you have too many useless tags,
third always use firebug in firefox, or press f12 in chrome.
you need to validate first not last. updated
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function () {
$('.error').hide('fast');
$('.warning').hide('fast');
$('.succes').hide('fast');
$('#register').submit(function () {
if ($('#name').val() == "")
{
$('.error').slideDown('slow'); return false;
}
else
{
var action = $(this).attr('action');
$(".states li").slideUp(750, function () {
$('.states li').hide();
$('#submit')
.after('<img src="images/ajax-loader.gif" class="loader" />')
.attr('disabled', 'disabled');
$.post(action, {
name: $('#name').val(),
realname: $('#realnam').val(),
pass: $('#pass').val(),
repeatpass: $('#repeatpass').val(),
mail: $('#mail').val(),
mailadres: $('#mailadres').val(),
});
});
return false;
}
});
});
</script>
<div class="text-section">
<h1>Aanmelden</h1>
<p>Hier kunt u zich <strong>gratis</strong> aanmelden op <?php echo $sNaam; ?></p>
</div>
<div class="states">
<ul class="states">
<li class="error"><strong>Fout</strong> Dit zullen ze zien wanneer ze iets fout hebben gedaan.</li>
<br />
<li class="warning"><strong>Waarschuwing</strong> Dit zullen ze zien wanneer er een onbekende fout is opgetreden.</li>
<br />
<li class="succes"><strong>Goed</strong> Dit zullen ze zien wanneer iets succesvol gegaan is.</li>
</ul>
</div>
<form method="POST" action="bin/register.php" id="register">
<fieldset>
<legend>Naam</legend>
<label id="username">Gebruikersnaam:</label>
<input type="text" class="text" name="gebruikersnaam" id="name" placeholder="Uw gebruikersnaam" /> <br />
<label id="realname">Uw echte naam:</label>
<input type="text" class="text" name="echtenaam" id="realnam" placeholder="Uw echte naam" /> <br />
</fieldset>
<fieldset>
<legend>Wachtwoord</legend>
<label id="password">Uw wachtwoord:</label>
<input type="password" class="text" id="pass" name="wachtwoord" placeholder="Uw wachtwoord" /> <br />
<label id="repeatpassword">Uw wachtwoord nogmaals:</label>
<input type="password" class="text" id="repeatpass" name="hwachtwoord" placeholder="Uw wachtwoord nogmaals" /> <br />
</fieldset>
<fieldset>
<legend>Mail</legend>
<label id="mailadres">Uw mail adres:</label>
<input type="text" class="text" name="mail" id="mail" placeholder="Uw mail adres" /> <br />
<label id="repeatmail">Uw mail adres nogmaals:</label>
<input type="text" class="text" name="hmail" id="mailadres" placeholder="Uw mail adres nogmaals" /> <br />``
</fieldset>
<input type="submit" name="submit" value="Registreren" class="orange" id="submit" />
<br />
</form>
demo link
I want upload a file with jquery ajax and php using forData() with this code:
var data = new FormData();
data.append('image',document.getElementById('uFile').files[0]);
data.append('tag','saveDocument');
data.append('data',$('#saveDocument').serializeArray());
$.ajax({
url: url,
type: 'post',
data: data,
cache: false,
contentType:false,
dataType: 'json',
processData: false,
success: function (data) {
setAlert("Documento guardado correctamente!",success);
}, error: function() {
setAlert("Ha ocurrido un error al guardar!",error);
}
});
return false;
This line contains all data of fields in my form:
data.append('data',$('#saveDocument').serializeArray());
But in PHP I can't access to that data and I want access to data of form to insert on a table, do you know what's the problem?
Html Form
<form id="saveDocument" enctype="multipart/form-data" method="post">
<p><i>Todos los campos son requeridos!</i></p>
<p>
<input id="uName" class="uName span5" name="uName" type="text" placeholder="Nombre completo" required/>
</p>
<p>
<input id="uEmail" class="uEmail span5" name="uEmail" type="email" placeholder="E-mail" required/>
</p>
<p>
<select id="uDept" class="uDept span5" name="uDept" type="text" required>
<option value="0">Seleccione departamento</option>
<option value="1">Dirección</option>
<option value="2">Recursos Humanos</option>
<option value="3">Oficina</option>
</select>
</p>
<p>
<input id="uIssue" class="uIssue span5" name="uIssue" type="text" placeholder="Asunto" required/>
</p>
<p>
<textarea id="uComment" class="uComment" name="uComment" placeholder="Comentario (Máximo 30 caracteres)" required></textarea>
</p>
<p>
<select id="uUrgency" class="uUrgency span5" name="uUrgency" type="text" required>
<option value="0">Seleccione urgencia</option>
<option value="1">Normal</option>
<option value="2">Alta</option>
<option value="3">Urgente</option>
</select>
</p>
<p>
<input id="uFile" class="uFile span5" name="uFile" type="file" required/>
<input id="nameFile" class="nameFile span5" name="nameFile" type="text" placeholder="Click para seleccionar el archivo" onClick="$('.uFile').click();"/>
</p>
<p>
<input class="btn btn-danger" type="reset" value="Limpiar"/>
<input id="sendFile" class="btn btn-primary" type="submit" value="Guardar"/>
</p>
The below image is taken from developers tool of chrome:
You can pretty easy. Take a look at these posts:
specially for images with demo
just all file type uploads with validation
I'm quite new to jquery but can't get the following to work properly. I can submit my form only once, after that i can't submit it a second time.
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$('#register').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(response) {
$('#regrespons').html(response);
$("#regrespons").css("visibility", "visible");
$("#regrespons").fadeOut(1000);
}
});
return false;
});
});
</script>
<form id="register" action="includes/register.php" method="post" autocomplete="off">
<input class="inp" name="gebr`enter code here`" type="text" />
<input class="inp" name="ww1" type="password" />
<input class="inp" name="ww2" type="password" />
<input class="inp" name="mail" type="text" />
<input class="but" type="submit" value="Registreren" />
</form>
Any help is apriciated!
Change $("#register").submit(function () { to $(document).on('submit', '#register', function () {