WordPress: How can use $_SESSION in wordpress - php

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']
?>

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);
}

Posting variable to ajax script

I got a function which displays an amount of products depending on a value that is selected in a dropdownlist. This is done with ajax, but for everything to work correctly I need to pass a variable.
On my catalog.php I got:
$pid = $productcatcr[0]['id'];
Which contains the page id.
On the same page I got the dropdownlist:
<select class="form-control showcount" name="showcount" id="showcount">
<option value="100" selected>Alle</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
</select>
Which works like this in ajax.js :
$("#showcount").on('change', function() {
$.post("ajax/getproducts.php", {start: ($("#showcount").val() - 4), end: $("#showcount").val()}, function(result){
$("#productviewajax").html(result);
});
});
Finally in getproducts.php I use the following query:
$product = "SELECT * FROM `web_content` WHERE `catid` = ".$conn->real_escape_string($_GET['id'])." AND state = 1 order by ordering LIMIT ".$_POST['end']."";
I need to post id from the initial page, through ajax to the query. What would be the best way to do this? My ajax is not very good.
I got another filter on the initial page on which I can just pass the variable in an url, but the other option element doesn't work like that.
Example:
<option value="highlow" data-post-url="prijshooglaag.php?id='.$pid.'">Prijs: Hoog naar laag</option>
Create one hidden input with class name as pageId (not mandatory with same class name, but if you are changing class name here. Change in Ajax code id:$(".pageId").val() here too. Both are related.)
catalog.php
<?php
$pid = $productcatcr[0]['id'];
?>
<input type='hidden' value='<?php echo $pid;?>' class='pageId'>
<select class="form-control showcount" name="showcount" id="showcount">
<option value="100" selected>Alle</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
<option value="12">12</option>
<option value="16">16</option>
<option value="20">20</option>
</select>
Use pageId to get current page id. And, pass it to getproducts.php page.
Ajax
$("#showcount").on('change', function() {
$.post("ajax/getproducts.php", {start: ($("#showcount").val() - 4), end: $("#showcount").val(),id:$(".pageId").val()}, function(result){
$("#productviewajax").html(result);
});
});
getproducts.php
Instead of $_GET['id'] use $_POST['id'] as $.post being used in AJAX.
$product = "SELECT * FROM `web_content` WHERE `catid` = ".$conn->real_escape_string($_POST['id'])." AND state = 1 order by ordering LIMIT ".$_POST['end']."";

How to get 'select' value and put into 'input' value?

I have a for with a select dropdown option. I want to get the value that is selected and put it into a hidden input value. I'm trying this at the minute -
echo '<form method="post" action="cart_update.php">';
echo"<select name='qty'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>";
echo '<input type="hidden" name="product_qty" value="'.$_GET['qty'].'" />';
echo"</form>;
When I submit this to cart-update.php it tells me that qty is an undefined index on the hidden input line.
Can anyone see the problem?
You will have to use javascript or jQuery. Some sample jQuery code:
$('select').change(function() {
var val1 = $('select').val();
$('your selector').val(val1);
});
Consider this instead:
$arr = array(1,2,3,4,5,6,7,8,9,10);
foreach($arr as $n){
$selected = $n == $_POST['qty'] ? 'selected' : '';
echo "<option value='$n' $selected>$n</option>";
}
Based on #Undefined_variable answer:
/***************************/
jQuery( document ).ready(function( $ ) {
/* selecy key from list , insert into field */
$('#select_start_key').change(function(){
var val1 = $('#select_start_key').val();
$('#start_key').val(val1);
/*reset the select to none*/
$('#select_start_key').val("");
});
});
View example :
https://jsfiddle.net/alinrzv/aj619Lbb/
A few problems:
1. Your form is in POST method, and you are trying to get the value in "Get" method ($_GET)
2. The value of $_GET will be available only after submitting the form.
You should use javascript to get the value. something like that:
<form method="post" action="cart_update.php">
<select name='qty' onChange='document.getElementById("product_qty").value = this.value'>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
</select>
<input type="hidden" id="product_qty" name="product_qty" value="1" />

How to send an array to php with jquery ajax?

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

how to concatinate a link with <select> <option> value on "onchange" event

<?php echo $this->getUrl(); ?> gives me my index page url .i.e. www.domain.com/index.php/
But now, what I want is, when any particular option is selected, it should add to this url.
.e.g.
www.domain.com/index.php/wordpress/
I write this code for it but now don know how to get it done. :(
<select onchange="setLocation('<?php echo $this->getUrl(); ?>')">
<option value=""> </option>
<option value="wordpress">Wordpress</option>
<option value="drupal">drupal</option>
<option value="magento">Megento</option>
</select>
Also, I had searched this link but couldn't take help from it. As it didnt work for me.
<select name="forma" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
With this (note that I've added this as the first argument):
<select onchange="setLocation(this, '<?php echo $this->getUrl(); ?>')">
<option value=""> </option>
<option value="wordpress">Wordpress</option>
<option value="drupal">drupal</option>
<option value="magento">Megento</option>
</select>
You could write the following JS:
function setLocation(elm, baseUrl) {
var keyword = elm.options[elm.selectedIndex].value; // Gets the selected keyword (some sanity check could be done)
window.location = 'http://' + baseUrl + keyword + '/'; // Redirects
}
Alternatively:
<select name="forma" onchange="window.location='http://<?php echo $this->getUrl(); ?>' + this.options[this.selectedIndex].value + '/';">
<option value="Home">Home</option>
<option value="Contact">Contact</option>
<option value="Sitemap">Sitemap</option>
</select>
(EDIT Prepended http:// to both examples.)

Categories