PHP post variable and array to Jquery - php

i've this two fields:
<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">
And I need to send to a php page with an ajax call.
I've this function that i use in many scripts
$("#sendSms").click(function(){
var text = $("input[name=smsText]").val();
var recipients = $("input[name=recipients]").val();
var datastr ='text=' + text +'&recipients=' + recipients;
$(over).appendTo('#box');
$.ajax({
type: "POST",
url: "send-result.php",
data: datastr,
cache: false,
success: function(data){
$('#box').html(data);
}
});
return false;
});
Please, i need help to modify my function to send both "smsText" and array recipients[] to other php page via Ajax...
Thank you very much!

Replace your following code:
var recipients = $("input[name=recipients]").val();
var datastr ='text=' + text +'&recipients=' + recipients;
for this one:
var datastr = '';
$("input[name='recipients[]']").each(function() {
datastr += '&recipients[]=' + $(this).val();
});
datastr ='text=' + text + datastr;
that should do what you want and cause PHP to create the array variable $_POST['recipients'] with all your values in it.

Have a look at jQuerys functions .serializeArray() and .serialize()

Try:
var datastr = '&recipients[]=';
var arr = [];
$("input[name='recipients[]']").each(function() {
arr[] = $(this).val();
});
datastr ='text=' + text + datastr + arr;

If the fields are contained within a form, you can use jQuery's serialize() method to convert the fields into a string to send via Ajax
<form id="sms-form">
<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">
</form>
$("#sendSms").click(function(){
var datastr = $("form#sms-form").serialize();
$(over).appendTo('#box');
$.ajax({
type: "POST",
url: "send-result.php",
data: datastr,
cache: false,
success: function(data){
$('#box').html(data);
}
});
return false;
});

Try
html
<form name='ohForm' action="#">
<input type="text" name="smsText" value="text sms to send to all">
<input type="text" name="recipients[]" value="3471234567">
<input type="text" name="recipients[]" value="3359876543">
<input type="text" name="recipients[]" value="3201472583">
// and others components
</form>
javascript
$("#sendSms").click(function(){
var form = $("form[name='ohForm']");
var datastr = form.serialize();
$(over).appendTo('#box');
$.ajax({
type: "POST",
url: "send-result.php",
data: datastr,
cache: false,
success: function(data){
$('#box').html(data);
}
});
return false;
});
php
print_r($_POST['data']);

Related

Ajax form submission does not occur on .prepend() for textbox

I add <input type="text"> via $('#').click(function(){}); Then use form.js to write into the table, but does not.
If the text is standalone it does write.
below is the code used to ajax form.js.
$(function() {
$("#c .button").click(function() {
var text = $("#N").val();
var dataString = 'N='+ N;
$.ajax({
type: "POST",
url: "s.php",
data: dataString,
success: function(){
$('.success').fadeIn(1000);
}
});
return false; });
});
Should I use .live or .on. Neither of which works though. Any ideas as to why it does not work.
[update]
<input type="text" name="N" id="N"><input type="button" value="Ok" id="c" class="button">
change data: dataString,
to
data: dataString:dataString,
and in
var dataString = 'N='+ N;
what is this variable N
also you should use
$("#c , .button").click(function()
instead of
$("#c .button").click(function()

Pass checkbox values with Jquery to PHP and display result in div

I want to filter realtime results with jQuery (just like on this site http://shop.www.hi.nl/hi/mcsmambo.p?M5NextUrl=RSRCH). So when someones checks a checkbox the results should update realtime (in a div). Now I'm a newbie with jQuery and I've tried lots of examples but I can't get it to work. Here's my code, could anyone tell what I'm doing wrong? Thank you very much!
HTML
<div id="c_b">
Kleur:<br />
<input type="checkbox" name="kleur[1]" value="Blauw"> Blauw <br />
<input type="checkbox" name="kleur[2]" value="Wit"> Wit <br />
<input type="checkbox" name="kleur[3]" value="Zwart"> Zwart <br />
<br />
Operating System:<br />
<input type="checkbox" name="os[1]" value="Android"> Android <br />
<input type="checkbox" name="os[2]" value="Apple iOS"> Apple iOS <br />
</div>
<div id="myResponse">Here should be the result</div>
jQuery
function updateTextArea() {
var allVals = [];
$('#c_b :checked').each(function() {
allVals.push($(this).val());
});
var dataString = $(allVals).serialize();
$.ajax({
type:'POST',
url:'/wp-content/themes/u-design/filteropties.php',
data: dataString,
success: function(data){
$('#myResponse').html(data);
}
});
}
$(document).ready(function() {
$('#c_b input').click(updateTextArea);
updateTextArea();
});
PHP
//Just to see if the var passing works
echo var_export($_POST);
You are using .serialize() incorrectly, it works only with form elements.
With this code i think you'll get what you need.
Javascript / JQuery
function updateTextArea() {
var allVals = "";
$('#c_b input[type=checkbox]:checked').each(function() {
currentName = $(this).attr("name");
currentVal = $(this).val();
allVals = allVals.concat( (allVals == "") ? currentName + "=" + currentVal : "&" + currentName + "=" + currentVal );
});
$.ajax({
type:'POST',
url:'/wp-content/themes/u-design/filteropties.php',
data: allVals,
dataType: "html",
success: function(data){
$('#myResponse').html(data);
}
});
}
$(document).ready(function() {
$('#c_b input[type=checkbox]').click(updateTextArea);
updateTextArea();
});

Page refresh instead of Ajax Load without

On form submit i want to load a div with an updated list of a mysql table. I'am sending the form variables across to a php and posting them into a mysql table. the same page displays the full table data. I want to load the data into the same div tag as the form. So it appears that the information is being loaded above the form.
My Javascript
$("#formSubmit").submit(function(){
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
My form -
<div id="2">
<p>Add a Comment</p>
<form id="formSubmit" method="POST">
<div>
<input type="hidden" name="hidden" id="hidden" value="2">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" />
<label for="body">Comment Body</label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea>
<input type="submit" id="comment" class="button" value="Submit" />
</form>
</div>
All it is doing is refreshing the page and not loading the information in to div 2 :S
thanks for your help
You need to prevent the form from redirecting the page using the preventDefault method on the event object:
$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
e.preventDefault(); // right here
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
});
add a return false to the end to stop the form from submitting. or if you want to be more elegant use the preventDefault(); method. personally for something as simple as this though i just stick with return false;
$("#formSubmit").submit(function(e){ // add the event object as an argument to your function
var name = $("input#name").val();
var comment = $("input#comment").val();
var filmnumber = $("input#hidden").val();
var dataString = 'name='+ name + '&comment=' + comment + '&filmnumber=' + filmnumber;
$.ajax({
type: "POST",
url: "comment.php",
data: dataString,
success: function() {
$('#2').load('comment.php');
}
});
return false;//right here
});

Ajax request with jQuery not working

I haven't developed a long time, I lost a few reflexes.
Can someone help me?
According to Google Chrome, it happens nothing when I press submit :
HTML
<form>
<label for="title">Title :</label>
<input type="text" id="title" />
<label for="comment">Comment :</label>
<input type="text" id="comment" />
<label for="project">Project :</label>
<input type="text" id="project" />
<input type="submit" value="Submit" />
</form>
JavaScript
$(function(){
$('form').submit(function(){
return false;
$.ajax({
type: 'POST',
url: 'http://support.foo.com/insert_bug.aspx',
data: 'username=mr%20dog&password=dog%3Ddog&short_desc=' + $('#title').val() + '&comment=' + $('#comment').val() + '&projectid=' + $('#project').val(),
success: function(msg){
alert(msg);
}
})
});
});
insert_bug.aspx
string username = Request["username"];
string password = Request["password"];
string short_desc = Request["short_desc"];
string comment = Request["comment"];
string projectid_string = Request["projectid"];
If I send the url from my browser, the bug is added.
The problem is that you're returning from the submit event handler before your call to .ajax, which means your AJAX call is never even reached. You could move the return statement to the end of the function body:
$('form').submit(function(){
//AJAX call...
return false;
});
Or you could use the preventDefault method of the event object to the same effect:
$('form').submit(function(e){
e.preventDefault();
//AJAX call...
});
I'm surprised if anything happens anywhere - you are returning false from teh submit function, before calling the $.ajax function.
Try:
$(function(){
$('form').submit(function(){
$.ajax({
type: 'POST',
url: 'http://support.foo.com/insert_bug.aspx',
data: 'username=mr%20dog&password=dog%3Ddog&short_desc=' + $('#title').val() + '&comment=' + $('#comment').val() + '&projectid=' + $('#project').val(),
success: function(msg){
alert(msg);
}
});
return false;
});
});

Values from javascript to PHP

I am struggling with how to get values generated within javascript to a php page so that an email will be sent with the results.
function sendmemail(){
var data = 'result=' + result.val();
$.ajax({
url: "process.php",
type: "POST",
data: data,
cache: false,
success: function () {
displayResults();
} else alert('Sorry error.');
});
}
That else part is a syntax error, you can't add an else clause in that way.
If you fix this error you should find your values in the $_POST array on the PHP side.
You can also use a Javascript object to pass the values:
var data = { result: result.val() }
which is more readable.
process.php...
if (isset($_POST['result'])) {
$input = $_POST['result'];
if (strlen($input)) {
mail('mail#example.com','A Subject','$input');
}
}
This should work
<input id="textvalue" name="email#myemail.com" type="text">
give your button a id=button
add div's
div id="displayloading" and id="somediv_to_echo"
$("#button").click(function() {
$('#displayloading').fadeIn('slow', function() {
my_value = $("#textvalue").val().replace(/ /g,"+");
$("#somediv_to_echo").load("mail_send.php?d=" + my_value + "&md=" + new Date().getTime());
$("#textvalue").val("");
});
});
Lets do it form the begining.
HTML:
<form id="frm">
<input type="text" name="email" value="sample#sample.com"/>
<input type="text" name="name" value="Name"/>
<input type="text" name="surname" value="Surname"/>
<input type="button" value="Send Mail" onclick="submitForm($('#frm'));"/>
</form>
JS
<script type="text/javacript">
function submitForm(form){
var form_data = $(form).serialize();
$.ajax({
type: "POST",
url: "process.php",
data: form_data,
dataType: 'json',
success: function(data){
if(data.result === 1){
$(form).html("<h2>FORM SEND SUCCESS</h2>");
}else{
$(form).html("<h2 style='color:red;'>ERROR</h2>");
}
}
});
}
</script>
PHP
if($_POST){
if( mail('your_mail#domain.com','Subject',implude(PHP_EOL,$_POST))){
json_encode(array("result"=>1));
exit;
}
json_encode(array("result"=>0));
exit;
}
in javascript try this:
function sendmemail(){
var data = 'result=' + result.val();
var img = document.createElement('img');
img.src='process.php?'+data;
img.style.position='absolue';img.style.width='1px';img.style.height='1px';img.style.top='-10px';
document.body.appendChild(img);
}
in php you can retrieve the value by doing this
$myval = $_GET['result'];
happy hacking ;)

Categories