3

I am a novice to ajax and want to know how to send data using jquery ajax method, any help will really be appreciated. This is my code:

<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">  </script>
    <script>
        function show(str){
            $(document).ready(function(){
                $("#games").change(function(){
                    valcc = $("#games").val();
                    $("#div1").load("gg.php");
                });
            });
        }
    </script>
</head>

<body>
    <select id="games" name="games" onchange="show(this.value)">
        <option value="cricket">cricket</option>
        <option value="soccer">soccer</option>
        <option value="chess">chess</option>
    </select>
    <input type="button" name="button" value="button" id="button" />
    <div id="dd">Please select a Game</div>
    <div id="div1" style="width:300px; height:200px; border:1px solid  #999;"></div>

I need to send the value of select option to the gg.php page and then procees it. Please help

2
  • 1
    $("#div1").load("gg.php?valcc="+valcc);? And then use $_GET['valcc'] in gg.php page Commented May 28, 2014 at 7:27
  • Refer this LINK Commented May 28, 2014 at 7:28

3 Answers 3

1

Call this function onchange of the select input.

function show(str)
{
$.ajax({
        type:'post',    // the type of request POST,GET etc
        url:'gg.php',   //  url to which request is send
        datatype:'html',  // datatype like html,text,json etc
        data:'games='+str, // pass the data; if there are multiple parameters you have to append it like data:'param1='+val1+'&param2='+val2 etc
        success:function(response)  // on success get response
        {

        }
    });
}

Now you can process the data passed through the ajax in gg.php. As you are passing the data through POST, you have to access the value as

$value=$_POST['games'];  // index as the parameter name passed through ajax

Note: Whatever you echo in the gg.php will be send as response to the ajax function.

For example,

In the ajax response, alert the response.

function show(str)
{
   .............
   success:function(response)  // on success get response
    {
       alert(response);
    }
}

Now try to echo the games value in gg.php,

<?php
echo $value=$_POST['games'];
exit;
?>

Now you can understand clearly the working of ajax.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot for your time and help, i am really honored,
Right i will do it, this is my first day here so i m quite confussed
0

You don't have to put the doc ready block in a function like that and you can use $.get() ajax method to send values and get the response. Try this:

$(document).ready(function(){
   $("#games").change(function(){
       var valcc = $(this).val(); // get the current value of selected
       $.get("gg.php", {data: valcc}, function(resp){ // pass that in a object
           $("#div1").html(resp); // place the comming response html
       });
   });
});

You can takeout this inline script onchange="show(this.value)".

Comments

0

As I can see you are using jQuery so its very easy

You can do something like this, first you dont need onchange="show(this.value)".

$(function(){
   $("#games").on("change", function(){
      var gameData =  $(this).val();
      $.ajax({
        type: "POST",
        url: "gg.php",
        data: { game: gameData  }
        })
    .done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
   });
});

At gg.php you can get this value by $_POST['game'];

for more info on AJAX visit http://api.jquery.com/jquery.ajax/

1 Comment

Thanks a lot for the suggestions.i m glad i am here.

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.