Clicking the buttonimage should go to search.php - php

<script>
function loadXMLDoc() {
$("input[class=search]").bind("keyup",function(){
$.get("search.php?search="+$(this).val(),
function(data){
if(data==1) {
alert("this is valid search")
} else {
alert("this is a right user search");
}
}
);
});
}
</script>
Below is the button image code
<table width="165" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<img src="images/search_box_left_im.png" width="3" height="28" />
</td>
<td class="inputbox_bg" width="118px">
<input type="text" name="search" class="username">
</td>
<td>
<input type="image" onclick="loadXMLDoc()" src="images/search_go_btn.png" border="0" width="44" height="28" />
</td>
</tr>
</table>
It is going to the function but not executing the ajax code

Change this:
<script>
function loadXMLDoc()
{
$("input[class=search]").bind("keyup",function(){
$.get("search.php?search="+$(this).val(),function(data){
if(data==1){
alert("this is valid search")
}else{
alert("this is a right user search");
}
})
})
}
</script>
to this:
<script>
function loadXMLDoc()
{
$("input[name=search]").bind("keyup",function(){ //<- important bit here
$.get("search.php?search="+$(this).val(),function(data){
if(data==1){
alert("this is valid search")
}else{
alert("this is a right user search");
}
})
})
}
</script>

$(document).ready(function(){
$(".username").keyup(function(event){ //<- important bit here
$.get("search.php?search="+$(this).val(),function(data){
if(data==1){
alert("this is valid search")
}else{
alert("this is a right user search");
}
});
});
$('input[type=image]').click(function() {
$('.username').keyup();
});
});

Please try this:
<script>
function loadXMLDoc() {
$.ajax({
type: 'GET',
url: 'search.php?search='+$(".username").val(),
success: function(data){
if(data==1) {
alert("this is valid search")
} else {
alert("this is a right user search");
}
}
});
}
</script>

Your <input type="image" ... /> is a submit button for the form.
What you want to do is change its default behaviour (submitting form) to alternative - execute some code.
Here is a good question about this: event.preventDefault() vs. return false
Besides that, you are assigning an event listener twice - once in your <input onclick="" /> and second time inside the event handler $("input[class=search]").bind()
Try removing your <input onclick="" /> and define event listener in $(document).ready():
<script>
$(document).ready(function() {
$("input[class=search]").bind("keyup",function(e){
e.preventDefault();
$.get("search.php?search="+$(this).val(),function(data){
if(data==1){
alert("this is valid search")
}else{
alert("this is a right user search");
}
});
});
});
</script>
...
<input type="image" src="images/search_go_btn.png" border="0" width="44" height="28" />
...

Related

whats is the error in ajax code

jqlib.js is https://code.jquery.com/jquery-3.2.1.js
nothing is happing i m trying to add to number using ajax using jquery 3.2.1.js
cant find the error in this code can anyone tell me where is the error in this code
add.php
<html>
<body>
<script type="text/javascript" src="jqlib.js"></script>
<form id="tt">
<table>
<tr>
<td>Enter first Number</td>
<td><input type=text name=t1 ></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type=text name=t2></td>
</tr>
<tr>
<td><input type=button value="OK" onClick="cal()"></td>
</tr>
<tr>
<td>Addition</td>
<td><input type=text name=t3 id="tt1"></td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
function cal()
{
var frm = $("#tt");
$.ajax(
{
type:"POST",
url:"ajax.php",
data:frm.serialize(),
sucess:function (data)
{
$("#tt1").val(data);
}
});
}
</script>
ajax.php
<?php
if(!empty($_POST))
{
if(($_POST['t1']!="" )|| ($_POST['t2']!=""))
{
$z = $_POST['t1'] + $_POST['t2'];
}
else
{
$z ="please enter data";
}
echo $z;
}
else
{
echo "please enter data";
}
?>
you have a typo error:
sucess should be success
Change your script as follow and check whether any alert is showing or not
<script type="text/javascript">
function cal()
{
var frm = $("#tt");
$.ajax(
{
type:"POST",
url:"ajax.php",
data:frm.serialize(),
sucess:function (data)
{
$("#tt1").val(data);
alert('Success: '+data); ///---> this show alert with success and content
},
error:function(a,b,c)
{
alert(c); //---> If error happen it will show alert();
}
});
}
</script>

Submit form without reload using jQuery AJAX in PHP MySQL

I have a basic signup/ login page that submits the data to the SQL database with php. However, I would like the page not to redirect on submission with help of jQuery AJAX (either successful or not).
This is what I currently have and it is not working. It doesn't show any error messages.
HTML - signup.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Signup</title>
<meta charset="utf-8">
</head>
<body>
<form>
<table>
<tbody>
<tr>
<td>
<input type="text" name="first" placeholder="First Name" id="first">
</td>
</tr>
<tr>
<td>
<input type="text" name="last" placeholder="Last Name" id="last">
</td>
</tr>
<tr>
<td>
<input type="submit" value="Signup" id="signup">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
JavaScript - signup.js
function submit() {
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
},
error: function() {
console.log("Signup was unsuccessful");
}
});
});
}
$(document).ready(function() {
submit();
});
PHP - signup.php
<?php
include_once "db_connect.php";
$post_FirstName = $_POST['first'];
$post_LastName = $_POST['last'];
$addData = "INSERT INTO details (firstname, lastname) VALUES ('$post_FirstName', '$post_LastName')";
if ($conn->query($addData) === TRUE) {
echo "Working";
} else {
echo "Not working";
}
?>
Here is the JSFiddle.
I hope you guys can help. Thanks in advance :)
If you are using ajax no need to use input type as submit use button.
$(document).ready(function() {
$("#signup").click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize()
success: function() {
console.log("Signup was successful");
}
error: function() {
console.log("Signup was unsuccessful");
}
});
});
Also change here
$post_FirstName = $_POST['first']; // name is `first` not `firstname`
You have some brakes and parentheses not properly closed
function submit() {
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
},//here
error: function() {
console.log("Signup was unsuccessful");
}
});});//here
}
$(document).ready(function() {
submit();
});
No need to call submit function. Just this will do, (you missed comma and closing tag):
<script>
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'signup.php',
data: $('form').serialize(),
success: function() {
console.log("Signup was successful");
}, //You missed this
error: function() {
console.log("Signup was unsuccessful");
}
});
}); //You missed this
</script>

Trying to update a database via AJAX and Jquery

I'm trying to achieve something basic, where you submit a form with a weight and it updates my database via ajax.
I've been looking through some of the answered questions for guidance but I just can't figure out why the below code doesn't work.
looking in chrome developer tools it look like it's not even submitting the form properly, so I don't think its the php script that's the issue (although it may have other issues once I fix the first problem).
Any help with fixing this would be much appreciated.
Here's the form and Ajax code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$("#weight_tracker_form").submit(function() {
$.ajax({
type: "POST",
url: "../weight_tracker_process.php",
data: {
weight: $("#weight").attr("value"),
},
success: function(){
alert("success");
},
error: function(){
alert("error");
}
});
return false;
});
</script>
<h2>Weight Tracker</h2>
<form id="weight_tracker_form" method="post">
Date: <input autofocus id="date" name="date" type="date"/>
Weight: <input id="weight" name="weight" type="text"/> Kg
<input type="submit" value="Submit Weight">
</form>
And here's the script weight_tracker_process.php
<?php
// configuration
require("includes/config.php");
if ($_POST["weight"] == NULL)
{
apologize("Please enter an email");
}
$weight = mysql_real_escape_string($_POST["weight"]);
$date = mysql_real_escape_string($_POST["date"]);
if (query("INSERT INTO weight (user_id, weight_measurement) VALUES (?, ?)", $_SESSION["id"], $weight); == false){
echo "Update Error";
}
else {
echo "Success";
}
?>
Thanks!
If this is your exact code, then what's probably happening is the handler isn't being attached to the form element. The JavaScript code is before the form, so when it runs the form doesn't exist yet. So this selector returns an empty array:
$("#weight_tracker_form")
You'll want to wait until the DOM is finished loading before assigning the event handler:
$(function () {
$("#weight_tracker_form").submit(function() {
$.ajax({
type: "POST",
url: "../weight_tracker_process.php",
data: {
weight: $("#weight").attr("value"),
},
success: function(){
alert("success");
},
error: function(){
alert("error");
}
});
return false;
});
});
Wrapping it in the jQuery function like this will cause it to wait until the document's ready event fires.
Try switch this:
weight: $("#weight").attr("value");
For this:
weight: $("#weight").val();
Also, you put the code on in document.ready. Like this:
$(document).ready(function({
$("#weight_tracker_form").submit(function() {
$.ajax({
type: "POST",
url: "../weight_tracker_process.php",
data: {
weight: $("#weight").attr("value"),
},
success: function(){
alert("success");
},
error: function(){
alert("error");
}
});
return false;
});
}));
Thanks guys, initial problem was as you suggested with me forgetting to wait for the dom to load before executing the script.
Second problem was some small bracket and syntax errors in the script.
Final code is below
HTML / Jquery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#weight_tracker_form").submit(function() {
$.ajax({
type: "POST",
url: "weight_tracker_process.php",
data: {
weight: $("#weight").val()
},
success: function(){
alert("success");
},
error: function(){
alert("error");
}
});
return false;
});
});
</script>
<h2>Weight Tracker</h2>
<form id="weight_tracker_form" method="post">
Date: <input autofocus id="date" name="date" type="date"/>
Weight: <input id="weight" name="weight" type="text"/> Kg
<input type="submit" value="Submit Weight">
</form>
Log Out
PHP
<?php
// configuration
require("includes/config.php");
// if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if ($_POST["weight"] == NULL)
{
echo "Error";
}
$weight = mysql_real_escape_string($_POST["weight"]);
if (query("INSERT INTO weight (user_id, weight_measurement) VALUES(?, ?)", $_SESSION["id"], $weight) == false){
echo "Update Error";
}
else {
echo "Success";
}
}
else
{
// else render form
printf("error");
}
?>
I also suggest to use jQuery.serialize() method to post all the fields from broser to the server.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#weight_tracker_form").submit(function() {
$.ajax({
type: "POST",
url: "../weight_tracker_process.php",
data: $("#weight_tracker_form").serialize(),
success: function(){
alert("success");
},
error: function(){
alert("error");
}
});
debugger;
return false;
});
});
</script>
</head>
<body>
<h2>Weight Tracker</h2>
<form id="weight_tracker_form" method="post">
Date: <input autofocus id="date" name="date" type="date"/>
Weight: <input id="weight" name="weight" type="text"/> Kg
<input type="submit" value="Submit Weight">
</form>
</body>
</html>

TypeError: Value does not implement interface HTMLInputElement

I'am trying to post a form using jQuery-ajax, and I'am getting this error on posting the form on click.
TypeError: Value does not implement interface HTMLInputElement
here is my JavaScript code:
$('document').ready(function () {
$('#update').click(function () {
jQuery.post("update_category", {
c_id: c_id,
c_name: c_name,
c_description: c_description
},
function (data, textStatus) {
if (data == 1) {
$('#response').html("Thank You!!..We Will Get Back To You Soon..!!");
$('#response').css('color', 'green');
} else {
$('#response').html("Some Error Occurred");
$('#response').css('color', 'red');
}
});
});
});
my form :
<div id="form">
<form>
<!-- PRONT DROP DOWN HERE !-->
<input type="text" name="c_id" id="c_id" disabled="disble" placeholder="'.strtoupper($r['c_id']).'" value="'.strtoupper($r['c_id']).'" ></input>
<input type="text" name="c_name" id="c_name" disabled="disble" placeholder="'.strtoupper($r['c_name']).'" value="'.strtoupper($r['c_name']).'"></input>
<textarea rows="4" class="field span10" name="c_description" id="c_description" disabled="disble" placeholder="Description">'.strtoupper($r['c_description']).'</textarea>
</form>
</div>
This error can be generated if you send a jQuery object in an ajax request.
So in your situation, it's likely that one of c_id, c_name, or c_description is a jQuery object representing an input field rather than the .val() value of the input element.
Your form containing server side PHP code within HTML code.
PHP code should be written as below.
<input type="text" name="c_id" id="c_id" disabled="disble" placeholder="<?php echo strtoupper($r['c_id']) ; ?> " value="<?php echo strtoupper($r['c_id']) ; ?> " ></input>
Also check the below link for Jquery reference.
Javascript: TypeError: Value does not implement interface FormData
try this
jQuery.post
use this
$.post
here full code
$(document).ready(function () {
$('#update').click(function () {
$.post("update_category", {
c_id: c_id,
c_name: c_name,
c_description: c_description
},
function (data, textStatus) {
if (data == 1) {
$('#response').html("Thank You!!..We Will Get Back To You Soon..!!");
$('#response').css('color', 'green');
} else {
$('#response').html("Some Error Occurred");
$('#response').css('color', 'red');
}
});
});
});
Hope it will help
Check the below code. span or div with id="response" is missing in your code and replace jquery.post by $.post. Give the file name update_category with the extension
<html>
<head>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$('document').ready(function(){
var c_id = '1', c_name = 'test', c_description = 'testing';
$('#update').click(function(){
$.post("update_category.php", {
c_id:c_id,
c_name: c_name,
c_description: c_description
}, function(data){
if(data == 1){
$('#response').html("Thank You!!..We Will Get Back To You Soon..!!");
$('#response').css('color','green');
}
else
{
$('#response').html("Some Error Occurred");
$('#response').css('color','red');
}
});
});
});
</script>
</head>
<body>
<div id="form">
<form>
<!-- PRONT DROP DOWN HERE !-->
<input type="text" name="c_id" id="c_id" placeholder="" value="" />
<input type="text" name="c_name" id="c_name" placeholder="" value="" />
<textarea rows="4" class="field span10" name="c_description" id="c_description" placeholder="Description"></textarea>
<input type="button" id="update" value="Update" />
<span id="response"></span> <!-- This is missing in your form -->
</form>
</div>
</body>
</html>
Try to use value inside variables
$('document').ready(function () {
$('#update').click(function () {
jQuery.post("update_category", {
c_id: c_id.val(),
c_name: c_name.val(),
c_description: c_description.val()
},
function (data, textStatus) {
if (data == 1) {
$('#response').html("Thank You!!..We Will Get Back To You Soon..!!");
$('#response').css('color', 'green');
} else {
$('#response').html("Some Error Occurred");
$('#response').css('color', 'red');
}
});
});

ajax on click update form content

This is my form.
If i click the status content like progress or dsgs a textfield should appear. If i type text in it and click outside or press enter the old content should updated with the new one. I need it to be done with ajax and php. I am a beginner in php and ajax. Any reference or how can i do this?
This is my code for add status
$insert_task = "INSERT INTO `tbl_task` (`intProjectid`,`intUserid`,`dtDate`,`dtFinishdate`,`varIssue`,`varStatus`,`varNeedhelp` )VALUES ('".$id."','".$userid."','".$dtdate."','".$dtfinish."','".$issue."','".$status."','".$help."');";
$insert_query=mysql_query($insert_task);
You didn't give me anything but I've tried to implement something by guessing and hope if it doesn't solve your problem but at least it will help you. Following code is for your ajax functionality, you can put it inside the head tag of your page between script tags-
$(document).ready(function(){
var eventFlag=false;
var originalText='';
$('#mytable tr td span').click(function(e){
e.stopImmediatePropagation();
$(this).siblings().show().focus();
$(this).hide();
eventFlag=false;
originalText=$(this).siblings().val();
});
$('#mytable tr td input').blur(function(e){
if(!eventFlag && validate($(this))) doAjax($(this));
else
{
$(this).siblings().show();
$(this).hide();
}
});
$('#mytable tr td input').keypress(function(e){
e.stopImmediatePropagation();
var code = (e.keyCode ? e.keyCode : e.which);
if(code==13)
{
if(validate($(this)))
{
doAjax($(this));
eventFlag=true;
}
else
{
$(this).siblings().show();
$(this).hide();
}
}
});
function validate(input)
{
console.log(input.val()+" "+originalText);
if(input.val()!='' && input.val()!=originalText)
return true
else return false;
}
function doAjax(input)
{
var formData="proId="+input.attr('id')+"&text="+input.val();
$.ajax({
type: "POST",
url: "update.php",
data: formData,
success: function(data){
if(data==1)
{
input.siblings().text(input.val()).show();
input.hide();
}
else
{
input.siblings().show();
input.hide();
alert("something Wrong !");
}
},
error:function (xhr, ajaxOptions, thrownError){
alert("Error:"+xhr.status+" "+thrownError);
}
});
}
});
And I guessed your form could be something like this
<form action="#" method="post">
<table id="mytable">
<thead>
<tr>
<th>Issue</th><th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>Login</td><td id="1"><span>Progress</span><input id="1" type="text" value="Progress" /></td>
</tr>
<tr>
<td>Something Else</td><td id="2"><span>Anything</span><input id="2" type="text" value="Anything"/></td>
</tr>
</tbody>
</table>
</form>
Put this inside your head or stylesheet (without style tag)
<style>
#mytable tr td input{display:none;}
</style>
And your update.php file should be something like
<?php
$proId = $_POST['proId'];
$text = $_POST['text'];
$update_task="update tbl_task set varStatus='".$text."' where intProjectid=".$proId;
if(mysql_query($update_task))
{
echo "1";
}
else
{
echo "0";
}
?>
I've tested it and working. The id I've used in the form is by assuming that you have id's for each of your status and you should update instead of inserting to change the status. let me know if it helps or if you need more help, I'll be on touch. Thanks!

Categories