0

I want to pass the jquery value "selected" to fetchdata.php without reloading the page. How can I do this?

Here is my code:

<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" 
        type="text/javascript"></script>

        <script src="http://code.jquery.com/ui/1.9.1/jquery-ui.min.js" type="text/javascript">
        </script>
        <script>
            $(document).ready(function() {
                $("#buttonClass").click(function() {
                    getValueUsingClass();
                });
            });
            function getValueUsingClass() {

                var chkArray = [];

                $(".chk:checked").each(function() {
                    chkArray.push($(this).val());
                });

                /* we join the array separated by the comma */
                var selected;
                selected = chkArray.join('#') + "#";

                if (selected.length > 1)
                {
                    $.ajax({
                        url: "fetchdata.php", //This is the page where you will handle your SQL insert
                        type: "GET",
                        data: "val=" + selected, //The data your sending to some-page.php
                        success: function()
                        {
                            console.log("AJAX request was successfull");
                        },
                        error: function()
                        {
                            console.log("AJAX request was a failure");
                        }
                    });

                    //alert("You have selected " + selected); 
                } else
                {
                    alert("Please at least one of the checkbox");
                }
            }
        </script>
    </head>
    <body>
        <div id="checkboxlist">
            <div><input type="checkbox" value="1" class="chk"> Value 1</div>
            <div><input type="checkbox" value="2" class="chk"> Value 2</div>
            <div><input type="checkbox" value="3" class="chk"> Value 3</div>
            <div><input type="checkbox" value="4" class="chk"> Value 4</div>
            <div><input type="checkbox" value="5" class="chk"> Value 5</div>
            <div>
                <input type="button" value="Get Value Using Class" id="buttonClass"> 
            </div>
</html>

fetchdata.php

<?php
    foreach($_GET['val'] as $r)
    {
        print_r($r);
    }
?>

I am using the GET method to receive the data and the for-each loop to print the array, but I am not getting any values in the PHP file.

3
  • Use print_r($_GET) to see what is comming in $_GET request Commented Aug 20, 2013 at 4:46
  • 1
    @user2376463, just as a heads up, it's a good idea to make sure that your code is formatted properly. It can make it easier to spot errors and minor issues that way! It may be a little overblown, but I'd suggest doing your PHP development work in something like NetBeans, then utilizing its code formatting tool heavily. Commented Aug 20, 2013 at 5:01
  • make sure selected has value Commented Aug 20, 2013 at 5:08

3 Answers 3

2

change the ajax function like below and make sure about the fectdata.php in the same folder or give the correct path.

$.ajax({
    url: 'fetchdata.php',
    type:'GET',
    data: {val:selected},
    success: function(data) {
        console.log("AJAX request was successfull");
    }
}); 
Sign up to request clarification or add additional context in comments.

11 Comments

+1 but maybe he sould use chkArray directly instead of selected, shouldnt he?
stil there is no improvement.. :(
by the way the following is always true: (selected.length > 1)
to check if you select at least one you shoud do if(chkArray.length > 0) instead of the string selected which has always at least one character (#) UPDATE: sorry you did > 1 so it should work
i would do data: {val:chkArray}, and see what happens in firebug console
|
1

Check if the path to your script is correct:

url: 'fetchdata.php',

Is this script in your doc root?

5 Comments

yes, check it in firebug console to see if the file is called properly
there doesn't seem to be anything wrong with your code, I'm getting the ajax response here.
@Linkmichiel please tell me more with examples
Use the path relative to the root directory. E.g.: if the scripts are located in the directory 'buttons', then you must also direct fetchdata.php to that directory, url: 'buttons/fetchdata.php'
You can also use a absolute path, url: 'http:// domain.com/buttons/fetchdata.php'
0

change the following in your code, in fetchdata.php

<?php 
$valArray = explode('@',$_GET['val']); 
foreach($valArray as $val){
echo $val."<br/>";
}
?>

In html File,

$(document).ready(function () {

$("#buttonClass").click(function() {
getValueUsingClass();
});

});

function getValueUsingClass(){ 
var chkArray = [];

$(".chk:checked").each(function() {
chkArray.push($(this).val());
}); 
/* we join the array separated by the comma */
var selected;
selected = chkArray.join('@') + "@";
if(selected.length > 1)
{ 
$.ajax({
url: "fetchdata.php", //This is the page where you will handle your SQL insert
type: "GET",
data: "val=" +selected.toString(), //The data your sending to some-page.php
success: function(data1) {  
$("#resultDiv").html(data1);
console.log("AJAX request was successfull");
},
error:function()
{
console.log("AJAX request was a failure");
}   
});

//alert("You have selected " + selected); 
}else
{
alert("Please at least one of the checkbox");   
}
}

include the div after a button like

<div id="resultDiv"></div>

2 Comments

Hi @srividhya there is no change..:( But the firebug console says "Ajax request was successfull"
add one div tag after a button. the data will be printed in the div

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.