Initialize jQuery Multiselect using AJAX - php

I have a problem with AJAX and JQUERY.
I have a form, which contents a tag. After select an option, I use AJAX to generate a formular, fill it with some data from a database and show it.
I would like to use the jQuery UI MultiSelect Widget
The problem is that I need to initialize the select with jQuery but it doesn't work when I call this function in the file that generates the new content: (it works well if I don't use AJAX).
<script type="text/javascript">
$(function(){
$("#ExampleSelect").multiselect({
selectedList: 4
});
});
</script>
The structure is the following:
PHP file with the main form which contains a normal and a which will contain the elements that are generated after the AJAX call. (onChange event in the <select>).
Ajax file
function LoadDiv()
{
var xmlhttp;
var serie_id = document.getElementById('serie_id').value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("divForm").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","divDataManagement.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("serie_id="+serie_id);
}
$.ajax({
success: function(){
$("#ExampleSelect").multiselect("destroy").multiselect({
selectedList: 4
});
}
});
PHP file that contains the jQuery UI MultiSelect which wil be displayed in the main php form (inside the <div> tag).
<select id = "ExampleSelect" multiple>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
Thanks for your help.

Initialize it in AJAX success method.
$.ajax({
...
success: function(){
$("#ExampleSelect").multiselect({
selectedList: 4
});
}
...
If you make changes on a selected instance of multiselect, destroy it and reinitialize after changing.
$.ajax({
...
success: function(){
$("#ExampleSelect").multiselect("destroy").multiselect({
selectedList: 4
});
}
...
In case you use XMLHttpRequest instead of jQuery ajax, you should init multiselect in onreadystatechange method.
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("divForm").innerHTML=xmlhttp.responseText;
$("#ExampleSelect").multiselect({
selectedList: 4
});
}
}

Related

Executing PHP with AJAX

I'm trying to run a php file with AJAX from my "main.php".
The problem is that it doesnt let my stay on the main.php and moves me to "tallennus.php", when I want to stay at "main.php". The tallennus.php should be done on the background.
"tallennus.php" does it's work perfectly (saves data to database). But I want to stay on mainsite while it does it.
Im trying to use AJAX to run "tallennus.php" on the background.
Why does the site change from main.php to tallennus.php?
<button onclick="testi()">testi</button><br>
<script>
function testi()
{
alert("function started");
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
}
xmlhttp.open("get", "tallennus.php",true);
xmlhttp.send();
}
</script>
The default type of <button> is type="submit", so it submits the form. Change it to type="button".
<button type="button" onclick="testi()">testi</button><br>
As per the following reference:
https://www.w3.org/TR/html-markup/button.html
"A button element with no type attribute specified represents the same thing as a button element with its type attribute set to "submit"."
I fixed my problem by doing this:
<script>
function tallenna()
{
var tunnus=document.getElementById('tunnus').value;
var nimi=document.getElementById('nimi').value;
var deadline=document.getElementById('deadline').value;
var vaihe=document.getElementById('vaihe').value;
var dataString=
'tunnus='+ tunnus +
'&nimi='+ nimi+
'&deadline='+ deadline+
'&vaihe='+ vaihe;
$.ajax({
type:"post",
url: "tallennus.php",
data: dataString,
cache:false,
success:function(html){
$('#msg').html(html);
}
});
document.getElementById("frm").reset();// for clearing form
return false;
}
</script>
<br><input type="submit" value="TALLENNA" onclick="return tallenna()">

How to pass value of a selected row in a popup div(which contains php include another file) on click of a button?

I have a form in which all the contacts are listed with fields like first name last name etc. Every row has view edit delete anchor tags on which I am calling a popup div. This popup div contains the php include(external file). I want to access the selected rows unique database primary key in that external file.
The link of the code is
https://www.dropbox.com/s/88qpmkwmaepa5s4/New%20Text%20Document%20%283%29.txt
Any help is appreciated.
Thank you
As a simple example to call Ajax by javascript and send the clicked row id to the php file, check this
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//returned response from ajax to the php page will be in xmlhttp.responseText
alert(xmlhttp.responseText); // Do whatever you need with the respones
}
}
var selected_row = document.getElementById('selected_row').innerText; // we assume this is the value of the selected row
xmlhttp.open("GET","path_to_your_phpfile&row_id=" + encodeURIComponent(selected_row), true);
xmlhttp.send();
}
Now the selected row id is passed by GET and in your php file you can get by $row_id = $_GET['row_id'];
If you are using jQuery, you go for a quick kill with AJAX.
Javascript:
<script>
function show_div(id) {
function show_lightbox(){
// your code
}
$.ajax({
type: "POST",
url: "get_detail.php",
data: "id=" + id,
dataType: "html",
success: function(html){
$("#show_detail").html(html);
}
});
}
</script>
Html:
<div id="lightbox">
<div id="show_detail"></div>
</div>
In your anchors:
<a onClick="show_div(uniqe_primarykey)">Click to fetch details</a>
In get_details.php
<?php
$id = $_POST[id];
//your queries
?>
<html>
Data from queries
</html>

multi pages in AJAX

I just built a web site by using this script:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
function loadpage(page)
{
document.getElementById("pageContent").innerHTML="Yükleniyor...";
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("pageContent").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",page,true);
xmlhttp.send();
}
</script>
It can load any page thanks to AJAX. But yet there is a question: when I load any page containing any HTML form, when i click "submit", it leaves the main page, I mean I can't send form variables by AJAX. the only thing I need is to pass form variables by using "href" and the loadpage() function I mentioned above.
How can I do get form input's values and send to another PHP file?
you can use jQuery.
$(document).ready(function(){
$("#div_load").load("page.html");
});
whit this code you can open any page (Ex: page.html) in any div(Ex:div whit id=div_load).
and for sending data use it:
$(".class_div").click(function(){
$.post("ajax.php",
{
name:"naser",
age:"23"
},
function(data,status){
// do something when done
});
});
As you are using jQuery, you can do:
$('form').submit(funciton() {
var data = $(this).serialize();
// Call Ajax
return false;
});
I advice you to read about:
http://api.jquery.com/category/ajax/ and http://api.jquery.com/serialize/.

Sending a value from an HTML select to PHP with JavaScript

The HTML with the select value is:
<select id="value_select" onchange="get_value_js()">
<option>Value 1</option>
<option>Value 2</option
</select>
The JavaScript code:
function get_value_js() {
var the_value = document.getElementById("value_select").value;
}
I don't know how to continue the JavaScript to send the value to, for example, work.php. In work.php, I want to store the value in a PHP variable.
You can use AJAX.
function get_value_js(){
var xmlhttp;
var e = document.getElementById("value_select");
var the_value = e.options[e.selectedIndex].value;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Do whatever with xmlhttp.responseText;
}
}
xmlhttp.open("GET","work.php?val="+the_value,true);
xmlhttp.send();
}
You can use $_GET['the_value'] to grab the value.
You need to enclose the <select> in a <form> tag like so:
<form id="myForm" method="post" action="work.php">
<select name="value_select">
<option value="1">Value 1 text</option>
<option value="2">Value 2 text</option
</select>
</form>
Please note that i've added value attributes to the <option> tags & added a name attribute to the <select>.
Next, you can submit the form in the following way:
function send()
{
document.getElementById( "myForm" ).submit();
}
Next in work.php:
<?
$value_select = $_POST[ "value_select" ];
echo( $value_select );
?>
You could either send it as an AJAX call if you want to stay on the same page or perform a redirect and pass the value as query string parameter:
window.location.href = '/work.php?value=' + encodeURIComponent(the_value);
Use ajax with jQuery
function get_value_js() {
var the_value =$("#value_select option:selected").val();
$.post('work.php?val='+the_value , function(data) {
//do whatever you want with the result
});
}

Javascript and php

I have a page called getvalues.php, I need to call a function which is written in a different php page(fileclass.php) in a class from this page(getvalues.php), but the issue is i need to pass a variable also which is $i, and the value of $i passed should be 1 if we are selecting option B, and $i=2 if option=c, and $i=3 if option=D given in dropdown. I had simply wiritten an onchange event but had not written any code in javascript. Please help Thanks. Here is the code for getvalues.php
<html>
<select id="s" onchange="callsome();">
<option value='B' selected>B</option>
<option value='c'>c</option>
<option value='D'>D</option>
</select></html>
<?php include("fileclass.php")
$obj=new file;
echo $obj->func($i);?>
You could implement this using JQuery or Javascript (I use JQuery in the example because it is shorter and easier to make Ajax calls) :
<html>
<select id="s" onchange="callsome();">
<option value='1' selected="selected">B<option>
<option value='2'>C</option>
<option value='3'>D</option>
</select>
<script>
function callsome() {
var selected = $('select#s option:selected').val();
$.ajax({
type: "POST",
url: "fileclass.php",
data: ({selectedvalue : selected}),
success: function(data) {
alert(data);
}
});
}
</script>
</html>
After that the callsome returns the output of the fileclass.php script and you can use that however you like in your code. From your explanation it was not really clear what is happening in fileclass.php and what you want to do with it, so I hope it helps you.
If you want the function in Javascript only:
<script type="text/javascript">
function callsome() {
var e = document.getElementById("s");
var strVal = e.options[e.selectedIndex].value;
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var data = xmlhttp.responseText;
//use the data as you wish
}
}
xmlhttp.open("GET","fileclass.php?selectedvalue=strVal",true);
xmlhttp.send();
}
</script>
This isn't the way PHP and HTML work.
PHP is rendered on the server. HTML is rendered on the client, after the PHP is completely done. To do what you want to do, you need to have the HTML (possibly Javascript) make a request to the PHP page at fileclass.php.

Categories