jQuery serialize form update MySQL based on form ID? - php

I populate a list of <form> using a PHP while loop, and fill the fields with values from MySQL. Now I want to be able to update all forms with one submit- button.
The form looks something like this in HTML (note, on my actual page there are multiple like this. The only thing that differes is the <form id='X'>:
<form id='34' name='customercontact' method='post' action='customerUpdate.php'>
Förnamn: <br /><input type='text' name='contact_firstname' class='textbox' value='text....'>
Efternamn: <br /><input type='text' name='contact_lastname' class='textbox' value='text....'>
Telefonnummer: <br /><input type='text' name='contact_phone' class='textbox' value='text....'>
Mobiltelefon: <br /><input type='text' name='contact_cellphone' class='textbox' value='text....'>
E-mail: <br /><input type='text' name='contact_email' class='textbox' value='text....'>
<select name='isActive'>
<option value="0" selected>Inaktiv</option>
<option value="1">Aktiv</option>
</select>
</form>
How do I use jQuery serialize to send each form and all the values as a string? I don'r really know where to start.

You can select all forms on the page $('form') and then iterate through them and 'submit' values via AJAX.
UPDATE:
<script type="text/javascript">
$(document).ready(function() {
$('#customer_contact_save').live('click', function(e){
e.preventDefault(); // prevent form submit
$.each($('form'), function(index) {
var sData = $(this).serialize(); // get data from form for submit
$.ajax({
type: "POST",// path to php script that saves data to db
url: "some.php",
data: sData,
success: function(someMessageFromPhp) {
alert(someMessageFromPhp); // alert to user what some.php returned
}
});
});
});
});
</script>

Related

send information via form without transaction in another page PHP

as i told, i need a form that send some information to another page but do not go to action page. is it possible? a form is like this:
<form name="form2" method="post" action="send.php">
<input type='text' name='name' id='name' value='ali'/>
<input type='text' name='id' id='id' value='123'/>
<input type='text' name='family' id='family' value='ali'/>
</form>
i want when i click on button, just send information. is it possible or needs to use of session?
Use ajax:
var name = $('#name').val();
var id = $('#id').val();
var family = $('#family').val();
$.ajax({
url : 'send.php',
type : 'post',
data : {name:'name',id:'id',family:'family'}
});

jquery using ajax to send data and save in php [duplicate]

This question already has answers here:
jQuery AJAX submit form
(20 answers)
Closed 8 years ago.
Good day im am trying to send or get data from a form and then using jquery and then ajax to send the data into a php page that should save it in the database how can i do it in jquery and use ajax to do it, any help will do and thanks!
HTML page 1 that will use jquery ajax to send data into the php page
<form>
Name:<input type='text' name='name'>
E-mail:<input type='text' name='email'>
Gender:<select name='gender'>
<option value='male'>male</option>
<option value='female'>female</option>
</select>
Message:<textarea name='about'></textarea>
</form>
PHP page 2 that would recieve the data from the page 1 form
<?php
echo "
$_POST['name'];
$_POST['email'];
$_POST['gender'];
$_POST['about'];
";
?>
any help with this project would help us greatly and thanks!
(update)
This is the jquery that i tried to use but it went to the url and i think that is not very safe
$(document).ready(function(){
$("#chat").click(function(){
$("#content").load("a.php");
});
$("#send").ajaxSubmit({url: 'a2.php', type: 'post'})
});
you can use following code :
var form = new FormData($('#form_step4')[0]);
form.append('view_type','addtemplate');
$.ajax({
type: "POST",
url: "savedata.php",
data: form,
cache: false,
contentType: false,
processData: false,
success: function(data){
//alert("---"+data);
alert("Settings has been updated successfully.");
window.location.reload(true);
}
});
where savedata.php is the file name in which you can do the the DB things
Hi i would start by adding a id to the form. and then either go with a onclick on the button element, or just define a click-event-handler for the button.
<form id="my_form">
Name:<input type='text' name='name'>
E-mail:<input type='text' name='email'>
Gender:<select name='gender'>
<option value='male'>male</option>
<option value='female'>female</option>
</select>
Message:<textarea name='about'></textarea>
<input type="button" value="Send" onclick="sendForm()"/>
</form>
Then the jquery/ajax/js part.
function sendForm(){
$.ajax({
type: "POST",
url: "PAGE2.php",
data: jQuery("#my_form").serialize(),
cache: false,
success: function(data){
/* alert(data); if json obj. alert(JSON.stringify(data));*/
}
});
}
Try this one:
<form id="formId">
Name:<input type='text' name='name'>
E-mail:<input type='text' name='email'>
Gender:<select name='gender'>
<option value='male'>male</option>
<option value='female'>female</option>
</select>
Message:<textarea name='about'></textarea>
<input type="button" value="Send" onclick="save()"/>
</form>
<script type="javascript">
function save(){
var query = $('#formId').serialize();
var url = 'savedata.php';
$.post(url, query, function (response) {
alert (response);
});
}
</script>
assign Id to your form... for now in my code i have given ID formId.. you can change this one as per your form name.

jquery post not working with HTML form

I am a beginner in Ajax and trying to build a simple ajax sample with jquery.
For some reason, my $.post is not working if I am putting my submit button in form tag.
Here is my creat_user.php:
<?php
$user_login = $_REQUEST['login_name'];
echo $user_login;
?>
Here is my html:
<form id='create_user_form'>
<p><label>Login Name : </label><input type='text' name = 'login_name'></p>
<p><label>Password : </label><input type='password' name='login_pw'></p>
<p><label>Re-Enter Password : </label><input type='password' name='login_pw2'></p>
<p><label>Email : </label><input type='email' name='email'></label></p>
<p><label>Date of Birth : </label><input type='date' name='date'></p>
<p><input type='submit' name='submit' id='create' value='create'></p>
</form>
And jquery:
$(document).ready(function(){
$("#create_user_form").submit(function(){
var serialize = $(this).serialize();
alert('Here');
$.post("create_user.php",serialize,function(response){
alert('Here2');
alert(response);
console.log("Response: "+response);
});
});
});
For some reason, the alert('Here2') is never fired, but it will work if I move the submit button out from the form tag, it works.(I do changed the selector in jquery code and changed submit() to click() ):
<form id='create_user_form'>
<p><label>Login Name : </label><input type='text' name = 'login_name'></p>
<p><label>Password : </label><input type='password' name='login_pw'></p>
<p><label>Re-Enter Password : </label><input type='password' name='login_pw2'></p>
<p><label>Email : </label><input type='email' name='email'></label></p>
<p><label>Date of Birth : </label><input type='date' name='date'></p>
</form>
<p><input type='submit' name='submit' id='create' value='create'></p>
SO, is that anything wrong from my code makes .post() not working if I put the submit button into form tag?
Maybe you need add
return false;
at the submit event?
You're missing return false or preventDefault() in the submit handler:
$("#create_user_form").submit(function(){
...
return false;
});
Or
$("#create_user_form").submit(function(e){
e.preventDefault();
...
});
This will prevent the browser from submitting the form after the ajax request is sent.
The default form submit is occurring prior to the Ajax call returning. You should prevent the default action of the form.
$(document).ready(function(){
$("#create_user_form").submit(function(e){
e.preventDefault(); //do not submit the form.
var serialize = $(this).serialize();
alert('Here');
$.post("create_user.php",serialize,function(response){
alert('Here2');
alert(response);
console.log("Response: "+response);
});
});
});

How to send value of checkbox and file from form with ajax

I have form and I want to send values from it with ajax to php script, but it doesn't send value of chceckbox if is it checked and doesnt't send file with avatar. So form send only values from text inputs, but not from checkbox and file. Can you help me please what is wrong?
Form:
<form action='' id='form1' method='post' name='form1' ENCTYPE='multipart/form-data'>
<input type='text' name='name' id='name' value=''>
<input type='text' name='age' id='age' value=''>
<input type='text' name='hobby' id='age' hobby=''>
<input type="checkbox" name="chkPHP" id="chkPHP" value="checked">
<input type='file' name='avatar' id='avatar' value='insert avatar' SIZE='30' accept=''>
<input type='submit' name='submit' id='submit' value='submit'>
</form>
Script to send values from form:
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script>
$(document).ready(function(){
$('#submit').click(function() {
$('#waiting').show(0);
$('#form1');
$('#error').hide(0);
$.ajax({
type : 'POST',
url : 'pksZpacuj.php',
dataType : 'json',
data: {
name: $('#name').val(),
age: $('#age').val(),
hobby: $('#hobby').val(),
chkPHP: $('#chkPHP').val(),
avatar: $('#avatar').val()
},
success : function(data){
$('#waiting').hide(0);
$('#error').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).show(0);
if (data.error === true)
$('#form1');
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
$('#waiting').hide(0);
$('#error').removeClass().addClass('error')
.text('There was an error.').show(0);
$('#form1');
}
});
return false;
});
});
</script>
In your ajax call, it may be easier to just send data: $('#form1').serialize() instead of each value separately.
FYI the file you send will be in the $_FILES array, not the $_POST in your php script.
Uploading files via AJAX is quite hard, and won't work in all browsers. I would strongly recommend against it.
If you really want to, you could follow this great post: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

get a forms values via jquery, post to a php script, and have php script process for errors and return value

I have this form:
<form action= 'addperm.php' class = 'addperm' method='post'>
<input type='text' name='user' /><br />
<input type='radio' name='perm' value='1' CHECKED /> Can view only<br />
<input type='radio' name='perm' value='2' /> Can make changes <br />
<input type='submit' value='Add'>
</form>
I want to know how, without redirecting to another page, I can get these two input values (user and perm), post them to a php script via ajax, and then have the php return the results
Read the jQuery API documentation for $.post.
$('input[type=submit]').click(function(e){
$.ajax({
type: "POST",
url: "some.php",
data: "user=" + $('input[name=user]').val() + "&perm=" + $('input[name=perm]:checked').val(),
success: function(msg){
// msg is your content
}
});
e.preventDefault();
});
some.php
echo json_encode(array($_POST['user'],$_POST['perm']));
I wouldn't take my syntax at face value, but that's the idea.
Download jQuery from http://jquery.com
Include it in the head of your page as such:
<script type="text/javascript" src="jquery/jquery.min.js"></script>
Change the src to whatever the name of the file you downloaded is.
Then also in the head of your page paste this:
<script type="text/javascript">
$(document).ready(function() {
$('.addperm').submit(function(){
var query = 'perm='+$('#perm').val()+'&user='+$('#user').val();
$.post("addperm.php", query, function(response){
alert(response);
});
});
});
</script>
To make this work you are going to have to change your HTML to this:
<form action= 'addperm.php' class = 'addperm' method='post'>
<input type='text' name='user' id='user' /><br />
<input type='radio' name='perm' value='1' id='perm' CHECKED /> Can view only<br />
<input type='radio' name='perm' value='2' id='perm' /> Can make changes <br />
<input type='submit' value='Add'>
</form>
I havent tested it yet, but it should work.

Categories