How to send an array to php with jquery ajax? - php

I tried to create an array with serializeArray and post it to php. but my code doesn't work. I read this questions (question) but I didn't understand my mistake yet.
this is my ajax code
var str = $("form").serializeArray();
$.ajax({
type: "POST",
url: "myfile.php",
data: str,
success: function (value) {
$("#mydata").html(value);
}
});
HTML Code
<form>
<select name="num0">
<option value="">num0</option>
<option value="12">12</option>
<option value="13">13</option>
</select>
<select name="num1">
<option value="">num2</option>
<option value="123">123</option>
<option value="133">133</option>
</select>
<select name="num2">
<option value="">num3</option>
<option value="12345">12345</option>
</select>
</form>
PHP Code
$postarr = array();
$num=$_POST['num0'];
$postarr[]=$num;
$num=$_POST['num1'];
$postarr[]=$num;
$num=$_POST['num2'];
$postarr[]=$num;
it giving me the following error message:
Notice: Undefined index: num0 (and same message for other variables).
By the way, English is not my native language; please excuse typing errors.

With the name attribute of an input field you are able to create an array in your $_POST:
<form method="post">
<select name="values[num0]">
<option value="">num0</option>
<option value="12">12</option>
<option value="13">13</option>
</select>
<select name="values[num1]">
<option value="">num2</option>
<option value="123">123</option>
<option value="133">133</option>
</select>
<select name="values[num2]">
<option value="">num3</option>
<option value="12345">12345</option>
</select>
</form>
In your php you can use it like this:
$postarr = $_POST['values'];
echo $postarr['num0'];

Try this:
$.ajax({
type: "POST",
url: "myfile.php",
data: $('form').serialize(),
success: function (value) {
$("#mydata").html(value);
}
});
and instead of
$num=$_POST['num0']
use
$num=filter_input(INPUT_POST,'num0');

Add an id to your form,
<form id="my_form">
<select name="num0">
<option value="">num0</option>
<option value="12">12</option>
<option value="13">13</option>
</select>
<select name="num1">
<option value="">num2</option>
<option value="123">123</option>
<option value="133">133</option>
</select>
<select name="num2">
<option value="">num3</option>
<option value="12345">12345</option>
</select>
Just change
var str = $("#my_form").serialize();
In your script

Related

jquery load passing a parameter

Hello just want to ask what is the proper way to pass a parameter using jquery my parameter is from a html input , below is my code.
function ajax_post(){
var param = document.getElementById('def').value;
$('#chatlogs').load('main.php?id='+param);
}
I'm able to load the main.php but cannot get to echo the param, here is my main.php code.
<?php echo $_GET['id'];?>
Adding more details.. im getting the parameters from a HTML select input then after getting the value, will then load the main.php into a div with a id of #chatlogs.
<select name="topic" id="def" class="form-control" style="width:450px;" onChange='ajax_post()'>
<option value="" selected>---</option>
<option value="Solder Short">Solder Short</option>
<option value="Insufficient Solder">Insufficient Solder</option>
<option value="Misaligned Component">Misaligned Component</option>
<option value="Missing Component">Missing Component</option>
<option value="Inverted Component">Inverted Component</option>
<option value="Pad Contamination">Pad Contamination</option>
</select>
<div id="chatlogs">
Please select a defect category...
</div>
Try this:
<select name="topic" id="def" class="form-control" style="width:450px;">
<option value="" selected>---</option>
<option value="Solder Short">Solder Short</option>
<option value="Insufficient Solder">Insufficient Solder</option>
<option value="Misaligned Component">Misaligned Component</option>
<option value="Missing Component">Missing Component</option>
<option value="Inverted Component">Inverted Component</option>
<option value="Pad Contamination">Pad Contamination</option>
</select>
<div id="chatlogs">
Please select a defect category...
</div>
<script>
$(document).ready(function(){
$('#def').on('change', function() {
var param = document.getElementById('def').value;
//alert(param);
$('#chatlogs').load('main.php?id=', param);
})
});
</script>
Try this one:
function ajax_post(){
var param = document.getElementById('def').value;
$('#chatlogs').load('main.php?id='+param);
}

how to display jquery/ajax data into select field?

code:
<script>
$(document).ready(function(){
$(".field").change(function(){
field = $(".field").val();
$.ajax({
type:"POST",
data:{"field":field},
url:"potential-courses.php",
success:function(data){
$(".course").val(data);
}
});
});
});
</script>
potential-courses.php
<?php
include("conn.php");
$field = $_POST['field'];
$sql = "select * from course_master where field = '$field' order by course_full_name";
$result = mysqli_query($link,$sql);
while ($row = mysqli_fetch_array($result))
{
echo "<option value=".$row['course_short_name'].">".$row['course_full_name']."</option>";
}
?>
html code:
<select name='field' class='field' id="field">
<option value="">Select Field</option>
<option value='engineering'>Engineering</option>
<option value='law'>LAW</option>
<option value='medical'>Medical</option>
<option value='management'>Management</option>
<option value='pharmacy'>Pharmacy</option>
<option value='hotel management'>Hotel Management</option>
<option value='mass communication'>Mass Communication</option>
<option value='agriculture'>Agriculture</option>
<option value='architecture'>Architecture</option>
<option value='education'>Education</option>
<option value='paramedical'>Paramedical</option>
<option value='design'>Design</option>
<option value='commerce'>Commerce</option>
<option value='film/tV/media'>Film /TV/ Media</option>
</select>
<select name="course" class="course">
<option value="">Select Courses</option>
</select>
In this code I have two dropdown list i.e
<select name='field' class='field' id="field">
and another is
<select name="course" class="course">
when I change value from "name=field" it display nothing in "name=course". where I am doing wrong please help me.
Thank You
Change it:
$(".course").val(data);
to
$(".course").html(data);
It will add the <option> set that you have returned from php to your <select>

WordPress: How can use $_SESSION in wordpress

I have this code to select user country like this :-
<form method="post" action="" enctype="multipart/form-data">
<select name="countryname" onchange="this.form.submit()">
<option value="">Select Country</option>
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<select>
Now I need to save the countryname in $_SESSION to use it in all my site page.
How can try this way in wordpress.
I f not support, How can set the page URL when select country to convert URL from
www.domain.com/
To
www.domain.com/?country=AD
To get it in $_GET value.
Please Try this
in your page at the top you can start the session :-
session_start();
after that here you have a form with method POST.
So,whatever value get in name="countryname" take this in Post method
like this:-
$country = $_POST['countryname'];
now your selected value store in $country ok.
after take this value in session like this:-
$_SESSION['country'] = $country
after that for check if in session value store or not
echo $_SESSION['country'];
and you get value in session
Krunal Trivedi was right. You need on select organize the ajax call and post the countryname. On server side you should take var from post and store at session
<form method="post" action="" enctype="multipart/form-data">
<select name="countryname" onchange="saveCountryCode()">
<option value="">Select Country</option>
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AS">American Samoa</option>
<option value="AD">Andorra</option>
<option value="AO">Angola</option>
<select>
jQuery Code:
function saveCountryCode(){
var selectedCountry = $('input[name="countryname"]').find(':selected').val();
$.ajax({
type: "POST",
url: "ajax/countrySaver.php",
data: { countryname: selectedCountry }
}).done(function( result ) {
alert(result);
});
}
countrySaver.php
<?php
session_start();
$_SESSION['countryname'] = $_POST['countryname'];
echo $_SESSION['countryname']
?>

Using AJAX or jQuery in WordPress to Return Results of PHP Script

I have an HTML form that accepts user input:
<form action="script_conv.php" method="post">
Convert: <input type="number" name="input" id="input">
<select name="unit1" id="unit1">
<option value="w">Words</option>
<option value="l">Lines</option>
<option value="p">Pages</option>
<option value="r">Recorded Minutes</option>
<option value="f">Finished Minutes</option>
</select>
to
<select name="unit2" id="unit2">
<option value="w">Words</option>
<option value="l">Lines</option>
<option value="p">Pages</option>
<option value="r">Recorded Minutes</option>
<option value="f">Finished Minutes</option>
</select>
<input type="submit">
</form>
The php script takes the user data and performs some calculations:
<?php
$input=$_POST["input"];
$unit1=$_POST["unit1"];
$unit2=$_POST["unit2"];
(bunch of if...else statements)
echo "<ul>";
echo "<li>$cat1f</li>";
echo "<li>$cat2f</li>";
echo "<li>$cat3f</li>";
echo "</ul>";
?>
This all works fine, but of course the results of the function are returned to a new page. I'd like to append the results of the function to the same page. I know this will require AJAX/jQuery, but nothing I've tried has worked. Can anyone give me some guidance?
Much appreciated!
Try test.php(html):-
<form method='post'>
Convert: <input type="number" name="input" id="input">
<select name="unit1" id="unit1">
<option value="w">Words</option>
<option value="l">Lines</option>
<option value="p">Pages</option>
<option value="r">Recorded Minutes</option>
<option value="f">Finished Minutes</option>
</select>
to
<select name="unit2" id="unit2">
<option value="w">Words</option>
<option value="l">Lines</option>
<option value="p">Pages</option>
<option value="r">Recorded Minutes</option>
<option value="f">Finished Minutes</option>
</select>
<input type="button" onclick="getvalue();">
</form>
test.php (script function):-
<script src="http://localhost:8004/js/jquery-1.9.1.js"></script>
<script>
function getvalue() {
var input= document.getElementById('input').value;
var unit1=document.getElementById('unit1').value;
var unit2=document.getElementById('unit2').value;
$.ajax({
type: "POST",
url: "http://localhost:8004/test/test.php",
data: { input: input,unit1:unit1, unit2:unit2,fun:'getvalue' }
}).done(function( data )
{
alert(data);
});
}
</script>
php code at the top of this (test.php)file:-
<?php if(isset($_REQUEST['fun']) == 'getvalue')
{
echo $_REQUEST['input'].$_REQUEST['unit1'].$_REQUEST['unit2'];
exit();
}
?>
You need to exit else it will display all the data from this file so use another way using php for doing in php
remove action from <form method="post"> and get response on the same page
Try this:
$(function(){
$('form').submit(function(e){
e.preventDefault();
$.post('/path/to/your/php', $(this).serialize(), function(response){
// do something with the response
});
});
});
Might be this will helpfull for you and fulfill your requirement
http://sharp-coders.blogspot.in/2012/06/submit-form-using-ajax-without.html

Javascript Jquery Get Dropdown value always 0

I am developing a website using CI, PHP, JQuery. I have a dropdown menu. When I try to get the value using JQuery, the result always 0. Here is my code.
html:
<div id="label"><label>Merk</label></div><div id="pilihan"><select name="brand">
<option value="0">PILIH SATU</option>
<option value="1">ACURA</option>
<option value="2">ALFA ROMEO</option>
<option value="3">ASTON MARTIN</option>
<option value="4">AUDI</option>
<option value="5">BENTLEY</option>
<option value="6">BERTONE</option>
<option value="7">BIMANTARA</option>
<option value="8">BMW</option>
<option value="9">BUGATTI</option>
<option value="10">BUICK</option>
<option value="11">CADILLAC</option>
<option value="12">CHANA</option>
<option value="13">CHERY</option>
<option value="14">CHEVROLET</option>
<option value="15">CHRYSLER</option>
<option value="17">CITROEN</option>
<option value="18">CODA</option>
<option value="19">DAEWOO</option>
<option value="20">DAIHATSU</option>
<option value="21">DAIMLER</option>
<option value="22">DATSUN</option>
<option value="23">DODGE</option>
<option value="24">DYNA</option>
<option value="25">FERRARI</option>
<option value="26">FIAT</option>
<option value="27">FORD</option>
<option value="28">FOTON</option>
<option value="29">GREELY</option>
<option value="30">GMC</option>
<option value="31">GREAT WALL</option>
<option value="32">HAGLER</option>
<option value="33">HINO</option>
<option value="34">HOLDEN</option>
<option value="35">HONDA</option>
<option value="36">HUMMER</option>
<option value="37">HYUNDAI</option>
<option value="38">INFINITI</option>
<option value="39">ISUZU</option>
<option value="40">JAGUAR</option>
<option value="41">JEEP</option>
<option value="42">KIA</option>
<option value="43">KTM</option>
<option value="44">LAMBORGHINI</option>
<option value="45">LAND ROVER</option>
<option value="46">LEXUS</option>
<option value="47">LIGIER</option>
<option value="48">LOTUS</option>
<option value="49">MASERATI</option>
<option value="50">MAYBACH</option>
<option value="51">MAZDA</option>
<option value="52">MCLAREN</option>
<option value="53">MERCEDES-BEN</option>
<option value="54">MINI</option>
<option value="55">MITSUBISHI</option>
<option value="56">MORGAN</option>
<option value="57">NISSAN</option>
<option value="58">OPEL</option>
<option value="59">PEUGEOT</option>
<option value="60">PORSCHE</option>
<option value="61">PROTON</option>
<option value="62">RANGE ROVER</option>
<option value="63">RENAULT</option>
<option value="64">ROLLS-ROYCE</option>
<option value="65">SAAB</option>
<option value="66">SKODA</option>
<option value="67">SMART</option>
<option value="68">SSANGYONG</option>
<option value="69">SUBARU</option>
<option value="70">SUNBEAM</option>
<option value="71">SUZUKI</option>
<option value="72">TATA</option>
<option value="73">TIMOR</option>
<option value="74">TOYOTA</option>
<option value="75">TRIUMPH</option>
<option value="76">VAUXHALL</option>
<option value="77">VOLKSWAGEN</option>
<option value="78">VOLVO</option>
<option value="79">WRANGLER</option>
</select></div>
JQuery:
$('select[name="brand"]').change(function(){
var brand = $("select[name='brand']").val();
alert(brand);
$.ajax({
type: "POST",
url: '<?php echo site_url("ajax_data/getmodel"); ?>',
dataType: 'json',
data: { vehicle_class : '', vehicle_brand : brand },
success: function(data) {
// Clear all options from vehicle class select
$('select[name="model"] option').remove();
// Fill vehicle class select
$.each(data, function(i, j){
var row = "<option value=\"" + j.model_id + "\">" + j.model_name + "</option>";
$(row).appendTo('select[name="model"]');
});
}
});
});
What is wrong with my code actually? Thank you.
I've just put your code in to a text file (named test.html)...
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
<div id="label"><label>Merk</label></div><div id="pilihan"><select name="brand">
<option value="0">PILIH SATU</option>
<option value="1">ACURA</option>
<option value="2">ALFA ROMEO</option>
<option value="3">ASTON MARTIN</option>
: : :
<option value="77">VOLKSWAGEN</option>
<option value="78">VOLVO</option>
<option value="79">WRANGLER</option>
<script type="text/javascript">
$('select[name="brand"]').change(function(){
var brand = $("select[name='brand']").val();
alert( brand );
});
</script>
</body>
</html>
And the code works, perfectly in FireFox, Chrome and IE8.
Also, my mate says salam sejahtera. :o)
You're selecting with select[name=brand], which means you're trying to access
<select value="THIS_IS_WHAT_YOU_ARE_SELECTING">
but you want the selected option, so use
$('select[name=brand] option:selected').val();

Categories