ajax not returning to php index page - php

pleae note that i m trying jump to test.php page from index.php/html using ajax and mysql, simple if text-input not found in table so it should go to test.php else stay on index.php/html page with ajax alerts, but from index page everytime receiving NOT AVAILABLE and sometime submit button not functional, below code FYR...
//index.php $(document).ready(function() {
//default form not submitted
$("#submit").data("submitted",false);
//preventing enter key in input field from submitting form
$("#welcome").on("submit", function(e){
if($('#submit').data("submitted")===false) {
e.preventDefault();
}
});
//trigger form submission
$("#submit").on("click",function(){
validate();
});});
//default form not submitted
//$("#submit
function validate() {
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
cache: false,
dataType: "json",
success: function(data) {
if(data){
alert("NOT AVAILABLE");
} else {
$("#submit").data("submitted", true);
$("#welcome").submit();
}
}}</script> <form action="check_test.php" method="post" name="welcome" id="welcome" /> <table width="550" border='0' align='center' cellpadding='0' cellspacing='0'> <tr>
<td align="center"><label>
Enter Inv. # *:
<input name="invst_num" type="text" id="invst_num" size="40" />
<input name="submit" type='submit' id='submit' /> </label></td> </tr></table> </form>
//check_test.php <?php
include ("config/config.php");
//get the username
if (isset($_POST['submit'])) {
$invst_num = $_POST['invst_num'];
//mysql query to select field username if it's equal to the username that we check '
$sql = mysql_query("select invst_num_ar from shareholders_ar where invst_num_ar = '$invst_num' ");
if ($result = mysql_num_rows($sql)>0) {
echo ($result);
}
}
?>
// if not found...test.php should load
<html>
<form
...
Register Data
/form>
</html>

Your conditional is backwards
if(data){
Should be
if(!data){
Or the alert should be flipped with the listener adding logic.

var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
ajax call is sending value frome data key, so you send only the text field content.
You probably should send sth like this:
$.ajax({
type: 'POST',
url: 'check_test.php',
data: { 'invst_num': num },
...
Open Dev tools in your browser and check what content is being sent during ajax call.

Related

AJAX submit no data on POST

I don't appear to have any data being sent when a form is submitted through jquery
A snippet of my issue (code reduced to simplify):
head:
<script>
function dSubmit(formName, formAction, fieldsToCheck, divToHide){
// No Errors process form
var url = formAction; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#" + formName).serialize(), // serializes the form's elements.
success: function(data)
{alert(data)}
});
return false; // avoid to execute the actual submit of the form.
}
</script>
body:
<a onclick="dSubmit('treatmentDetails','treatments/processTreatments.php','','')"><img style="width:35px;" src="i/update-icon.png"/></a>
<form name="treatmentdetails" id="treatmentdetails"
action="processTreatments.php" method="POST">
<input type="text" name="treatmentName" value="<?php echo $treatment;?>"/>
<input type="text" name="treatmentId" value="<?php echo $treatmentId;?>"/><br>
</form>
php (treatments/processTreatments.php):
$table = $dbPrefix."treatmentsOptions";
$treatmentId = $_POST['treatmentId'];
$treatmentName = $_POST['treatmentName'];
$query = mysql_query("UPDATE $table SET name='$treatmentName' WHERE id='$treatmentId'");
echo "variables = id: $treatmentId name: $treatmentName"; // according to jquery submit neither variable appear
You typo treatmentDetails, fix to treatmentdetails.
$("#treatmentDetails").serialize() is return empty string.
your correct code is:
<a onclick="dSubmit('treatmentdetails','treatments/processTreatments.php','','')"><img style="width:35px;" src="i/update-icon.png"/></a>

Using jquery ajax to serialize data and submit into mysql database

The problem i have is that whenever it inserts the data into the database it doesn't redirect the user to the invoice.php page. Please guys i really need your help.
This is the html code:
<form method="POST" action="">
<input type="text" name="resident_address" id="resident_address"/>
<input type="text" name="price" id="status"/>
<input type="hidden" name="status" id="status" value="0"/>
</form>
This is the jquery code:
var dataString = $('#applyform').serialize();
$.ajax({
type: "POST",
url: "applyform.php",
cache: false,
data: dataString,
beforeSend: function()
{
$(".apply_error").hide();
},
success: function(html) {
if (html == "true")
{
// You can redirect to other page here....
window.location.href = 'invoice.php';
}
else
{
//window.location.href = 'apply.php';
$("div.apply_error").html("Wrong details").show();
}
}
});
this is the php which is the applyform.php:
if(isset($_POST['Submit']))
{
$result = mysql_query("INSERT INTO mytable (resident_address, price, status) VALUES ('$addressfields', '$price', '$status')");
if($result){
echo "true";
}
}
you are not posting a POST var called "Submit" so your
if(isset($_POST['Submit']))
will always evaluate to false and your mysql query is never executed.

how to get validation errors through ajax in codeigniter

I'm trying to use ajax within my submit form in a codeigniter. I have it to where the ajax call is made, bu the validation errors are not displaying. I can't figure out why. Please help.
I do have some returns in there, but they do nothing.
if ($this->form_validation->run() == FALSE)
{
echo(json_encode("validate"=>FALSE));
}
else
{
$this->load->model('adduser_model');
$data['query']=$this->adduser_model->adduser();
}
}
view code:
<script>
//CHECKS ONE FIELD AT A TIME
$(function(){
$(".field").each(function(){
$(this).keyup(function(){
var id = $(this).attr("id"); //VALUE OF INPUT ID Ex: <input id="name">
var v = $(this).val(); //INPUT TEXT VALUE
var data = id+"="+v; //DATA TO GO TO THE AJAX FILE Ex:(name=wcet)
$.ajax({
type: "POST",
url: "prog/validate", //AJAX FILE
data: data+"&single=true",
success: function(e){ //"e" IS THE DATA FROM "validate.php"
$("span#"+id).html(e); //ECHOS DATA FROM "validate.php" NEXT TO THE INPUT IF NEEDED
}
});
});
});
});
</script>
<BODY>
<?php $this->load->helper('form');
echo form_open('prog/validate'); ?>
<tr><td align="right">Name: </td><td align="left"><input class="field" name="name" id="name"> <span id="name"></span><br></td></tr>
<tr><td align="right">email: </td><td align="left"><input class="field" name="email" id="email"> <span id="email"></span><br></td></tr>
If you have some returns that means the function was successful. Even if you didn't have the response you expected, the $this->form_validation->run() will be false ONLY if the $ajax call couldn't fire at all (404, 500 errors).
You can also trying catching the error via failure: function () {}, for example:
$.ajax({
type: "POST",
url: "prog/validate", //AJAX FILE
data: data+"&single=true",
success: function(e){ //"e" IS THE DATA FROM "validate.php"
$("span#"+id).html(e);
},
failure: function(e) {
// check against error messages
}
});

Ajax call only if the jquery validation is valid

I have a form and ajax call.data are filled in form and goes to remote at the same time user is created in my database using ajax call.but the problem is with validation,which validation should apply here
for example i apply any jquery validation i need to test if the form is valid and then i can call ajax if the form is valid other wise the database will be filled with blank data
i tried with bvalidator but i don't know how to detect the form is valid or invalid using any jquery validator library
<form id="inform" action="http://site.com">
<input type="text" id="name" />
<input type="text" id="email" />
<input type="button" name="subaction" id="infuse" />
</form>
<script language ="javascript" type = "text/javascript" >
$na= jQuery.noConflict();
$na(document).ready(function(){
$na('#infuse').click(function(){
var name= $na('#name').val();
var email = $na('#email').val();
$na.ajax({
type: "POST",
url: '<?php bloginfo('template_url')?>/user_create.php',
data: 'name='+name+'&email='+email,
cache: false,
success: function(result){
jQuery('.err_msg').html(result);
jQuery("#inform").submit();
}
});
});
});
</script>
You could do something like this
<script language ="javascript" type = "text/javascript" >
$na= jQuery.noConflict();
var name;
var email;
var options10 = {
// shows or hides error all messages container after validation completes
onAfterAllValidations: function(elements, formIsValid){
// if validation failed
if(!formIsValid)
callAjax();
else{
//do something
}
}
};
function callAjax(){
$na.ajax({
type: "POST",
url: '<?php bloginfo('template_url')?>/user_create.php',
data: 'name='+name+'&email='+email,
cache: false,
success: function(result){
jQuery('.err_msg').html(result);
jQuery("#inform").submit();
}
});
}
$na(document).ready(function(){
$('#inform').bValidator(options10);
$na('#infuse').click(function(){
name= $na('#name').val();
email = $na('#email').val();
});
});
</script>
I haven't checked this, but hopefully , this works. The important point is, you could use the callbacks : onAfterAllValidations.

Send single input with ajax/jquery/php

I'm new to jQuery / AJAX.
I'm trying to send single input with jquery/ajax/php.
LIVE EXAMPLE
But, after pressing submit nothing is happening, where is my error?
Any help much appreciated.
HTML:
<form action="submit.php">
<input id="number" name="number" type="text" />
<input id="submit" name="submit" type="submit" />
</form>
JQUERY / AJAX:
$(document).ready(function(e) {
$('input#submit').click(function() {
var number = $('input[name=number]');
var data = 'number=' + number.val();
$.ajax({
url: "submit.php",
type: "GET",
data: data,
cache: false,
success: function(html) {
if (html == 1) {
alert('wyslane');
}
else {
alert('error');
}
}
});
return false;
});
});
PHP:
<?php
$mailTo = 'email#gmail.com';
$mailFrom = 'email#gmail.com';
$subject = 'Call Back';
$number = ($_GET['number']) ? $_GET['number'] : $_POST['number'];
mail($mailTo, $subject, $number, "From: ".$mailFrom);
?>
HTML:
<form id=submit action="">
<input id="number" name="number" type="text" />
<input name="submit" type="submit" />
</form>
The action URL is irrelevant as you want to submit your data via AJAX. Add the submit id to the form and override the default submit behavior, instead of overriding the onclick handler of the submit button. I'll explain in the JS section.
JS:
var number = $('input[name="number"]');
Quotes were missing.
$(document).ready(function(e) {
$('#submit').submit(function() {
var number = $('input[name=number]');
var data = 'number=' + number.val();
$.ajax({
url: "submit.php",
type: "GET",
data: data,
cache: false,
success: function(html) {
if (html == 1) {
alert('wyslane');
}
else {
alert('error');
}
}
});
return false;
});
});
I don't really understand your success callback, why do you expect that html should be equal to 1?
Atleast I got 404 error when pressed your submit button:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
When you get it to work, remember to add mysql_real_escape_string function to avoid SQL injections http://php.net/manual/en/function.mysql-real-escape-string.php
Since you are also using ID for number, you could just use: var data = 'number=' + $('#number').val()
Also if you add ID to your form, you can use:
$('#formId').submit(function(){
});
instead of that click. This function will launch when that form is submitted. This is better way because users can submit the form with other ways aswell than just clicking the submit button (enter).
var number = $('input[name=number]');
is wrong. It's
var number = $('input[name="number"]');

Categories