I’m trying to get the value of the first value pair from a form submission via AJAX. Here’s what the var Formdata looks like (id=4&name=somename&blah=blah). How do I get the id value of 4? As you can see I was trying several ways but had no luck. Any suggestion would be much appreciated, thanks in advance.
$("#updatetask").validate({
submitHandler: function() {
var formdata = $('#updatetask').serialize();
var fistkey = formdata.split("&",1);
var tester = fistkey.slice(-1);
alert(tester);
/*
$.post('/tasks/AJAXupdate', $('#updatetask').serialize(), function(data){
var returnMsg = data.replace(/^\s+|\s+$/g, '');
if (returnMsg == 'error'){
alert(returnMsg+': Unable to update task.');
}else{
parent.$.fancybox.close();
}
});*/
return false;
}
});
});
Can't you simply $('#updatetask :input:eq(0)').val() instead of serializing it and parsing the string?
Related
It is fully functional. I want to check all the data. Using this, I can't see all the data.
alert("SHOW FORM DATA");
alert(PreparedFormObj());
FORM DATA
var PreparedFormObj = function () {
var _FormData = new FormData()
_FormData.append('Id', $("#Id").val())
_FormData.append('CreatedDate', $("#CreatedDate").val())
_FormData.append('CreatedBy', $("#CreatedBy").val())
_FormData.append('ProductId', $("#ProductId").val())
_FormData.append('ProductModelNo', $("#ProductModelNo").val())
_FormData.append('Name', $("#Name").val())
return _FormData;
}
SAVE DATA
var SaveProduct = function () {
alert("SHOW FORM DATA");
alert(PreparedFormObj());
}
for example convert to json
alert(JSON.stringify(Object.fromEntries(PreparedFormObj())));
What I want to achieve:
I need to use POST method then after success, it will locate to another url of ajax along with the POST data.
This is what I have done so far:
<script>
$("#btnSubmit").click(function(){
var ddate = $("#ddate").val();
var empid= $("#empid").val();
$.ajax({
type:'POST',
url:'../PaySlip/view_payslip.php',
data:{numnumnum:empid,dateito:ddate},
success:function(){
window.location ="../PaySlip/view_payslip.php";
}
});
});
Problem:
Upon success, the view_payslip cannot gather the POST data that I have transferred. I know what I did was wrong. That is why I'm asking for help to how to get this right.
Thank you in advance.
What you need to do is to post dynamic form in success callback.
I have also similar requirement in one of my project and for that I have created a method similar to shown below.
In this method you need to pass URL, windows option and params which is your data object, name is the window name, if you want to open in same window then instead of opening a new window you can pass blank in name param and omit the window.open() statement. You can modify it as per your requirement.
function submitDynamicForm(url, windowoption, name, params)
{
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", url);
form.setAttribute("target", name);
for (var key in params) {
if (params.hasOwnProperty(key)) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = key;
input.value = params[key];
form.appendChild(input);
}
}
document.body.appendChild(form);
window.open("", name, windowoption);
form.submit();
document.body.removeChild(form);
}
First of all, Hello Everyone, I'm new in here.
I have searched all over the site and I can't seem to find an answer to my problem...
I have an input, being generated via AJAX-PHP dynamically, and I want the user to enter some Value there, then send the data via Ajax for processing it into Mysql.
Thing is, javascript is reading the input value of the as Null:
"Uncaught TypeError: Cannot read property 'value' of null"
Here's the code:
function addBandMember() {
var Btn = _("addBandMem");
var errorMsg = _("UpWinMsg");
var url = window.location.href;
var mmbr = _("#newBandMember").value; // <-THIS IS RETURNING NULL
if (mmbr == "") {
errorMsg.innerHTML = "Please enter a USERNAME";
} else {
errormsg.innerHTML = "please wait...";
var ajax = ajaxObj("POST", url);
ajax.onreadystatechange = function() {
if (ajaxReturn(ajax) == true) {
//SETTINGS RECEIVED
if (ajax.responseText != "member_added") {
errorMsg.innerHTML = ajax.responseText;
} else {
toggleUpWin();
receiveUserData();
}
}
};
ajax.send("mmbr=" + mmbr);
}
}
I believe this is happening because the Input field is being generated AFTER the document loads, via Ajax and PHP... Am I wrong? How can I address this newly-generated ID?
Thanks!
Try this one to get the value of input box.
var mmbr =$("#newBandMember").val();
OR
var mmbr =_("#newBandMember").val();
Found the answer... It was just a stupid mistake...
I have a function set for "getElementById" which requires a string, without "#".
so it'd be:
var mmbr = _("newBandMember").value; // <-THIS IS NOT RETURNING NULL
instead of:
var mmbr = _("#newBandMember").value; // <-THIS IS RETURNING NULL
Thanks to everyone who answered!
Formulated my options string using .each and sending it via .post to an external script.
clicked.closest("form").find("input,textarea").each(function(){
var input=$(this);
if(index==1){
options = "{"+input.attr("name")+":'"+input.val()+"'";
}else if(index==no_inputs){
options += ","+input.attr("name")+":'"+input.val()+"'}";
}else{
options += ","+input.attr("name")+":'"+input.val()+"'";
}
index++;
});
alert(options);
$.post('../pages/ajax/add_orders.php',options,function(data){
alert(data);
});
..problem is when my php call script is..
<?php
print_r($_POST);
?>
I simply get an empty array with no values.
The first alert shows the formulated string correctly.
But the second one returns an empty value.
Any help greatly appreciated.
You're post needs to be an actual object, you're creating a string.
clicked.closest("form").find("input,textarea").each(function(){
var input=$(this);
if(index==1){
options[input.attr("name")] = input.val();
}else if(index==no_inputs){
options[input.attr("name")] = input.val();
}else{
options[input.attr("name")] = input.val();
}
index++;
});
I think the above way will work.
try this instead:
var options={};
clicked.closest("form").find("input,textarea").each(function(){
var input=$(this);
options[input.attr("name")] = input.val();
});
I'm trying to fill a form if I find that the name and the firstname input by the user already exist in my database.
For the moment, I would like to check if the name and the firstname already exist in the database but my javascript program doesn't work. It's seems that the part..." "$.post("../modules/verifier_staff.php",{ nom: ..." is not correct.
Then I would like to fill my form with the data that my database give (array).
Can somenone help me to find my mistakes and to tell me how I can use the data i receive from database to fill my form?
$(document).ready(function(){
var form_nom = $("#staff_nom");
var form_prenom = $("#staff_prenom");
$(form_nom).change(function() {
checkvariable();
});
$(form_prenom).change(function() {
checkvariable();
});
function checkvariable(){
if((form_nom.val().length >0)&&(form_prenom.val().length>0)){
prenom_staff = form_prenom.val();
nom_staff = form_nom.val();
//alert(nom_staff);
$.post("../modules/verifier_staff.php",{ nom: nom_staff, prenom: prenom_staff},
function(data){
if(data)
{
alert('ok');
}
else alert('nok');
});
}
}
});
var form_nom = $("#staff_nom");
var form_prenom = $("#staff_prenom");
$(form_nom).change(function() {
checkvariable(form_nom,form_prenom); //pass arguments into the function
});
$(form_prenom).change(function() {
checkvariable(form_nom,form_prenom);
});
function checkvariable(form_nom,form_prenom){ //innclude argument in function definition
//......
}
thank for your help. Yes I'm sure that the path is correct.
Here is my code from ../modules/verifier_staff.php
<?php
include('../inc/settings.php');
include('../inc/includes.php');
//$_POST['nom']='Doe';
//$_POST['prenom']='John';
$nom=$_POST['nom'];
$prenom=$_POST['prenom'];
$Dbcon = new Db;
$personne_manager = new PersonneManager($Dbcon->dbh);
$personne = $personne_manager->get(array('conditions'=> array('nom' => strtolower($nom),'prenom' => strtolower($prenom))));
if($personne)
{
$result = true;
}
else
{
$result = false;
}
return $result;
?>
this part works well (I tested it) so I think the problem come from my javascript code. I'm a newbie in javascript/jquery.