1

I am trying to make a set of webpages that will display a unique graph based on a simple form that only has a selector box and a submit button. Basically what I want to happen is when the user changes the month in the selector and presses submit, a new chart set will render on the same page.

Here is the HTML initial page:

<HTML>
<HEAD>
    <SCRIPT src="http://code.jquery.com/jquery-1.10.1.min.js"></SCRIPT>
</HEAD>
<BODY>
    <CENTER>
        <FORM ID="form1" METHOD="post" ACTION="">
            <SELECT NAME="monthSelector">
                <OPTION VALUE="0">Select Month...</OPTION>
                <OPTION VALUE="1">January</OPTION>
                <OPTION VALUE="2">February</OPTION>
                <OPTION VALUE="3">March</OPTION>
                <OPTION VALUE="4">April</OPTION>
                <OPTION VALUE="5">May</OPTION>
                <OPTION VALUE="6">June</OPTION>
                <OPTION VALUE="7">July</OPTION>
                <OPTION VALUE="8">August</OPTION>
                <OPTION VALUE="9">September</OPTION>
                <OPTION VALUE="10">October</OPTION>
                <OPTION VALUE="11">November</OPTION>
                <OPTION VALUE="12">December</OPTION>
            </SELECT>
            <INPUT TYPE="submit" VALUE="Show Charts">
        </FORM>

        <DIV ID="response"></div>

        <SCRIPT>
            function submit()
            {
                $(function()
                {
                    var month = 3;
                    var formdata = "month=" + month;
                    $.ajax({
                        type: 'POST',
                        url: 'showCharts.php',
                        data: formdata,
                        success: function(data) {
                            $("#response").html(data);
                        }
                    });
                });
            }
        </SCRIPT>
    </CENTER>
</BODY>
</HTML>

and here is showCharts.php:

<?php
    include("../FusionCharts/FusionCharts.php");
    include("../DBConnect.php");

    $month = $_POST['month'];
    echo $month;
    //insert complex queries and fusioncharts code that already works!
?>

Someone please help me, I've been staring at this for hours and can't make any progress.

4
  • 2
    You're using NAME="monthSelector" and $_POST['month'] try changing it to NAME="month" that ought to fix it. Commented Jun 30, 2014 at 18:17
  • @Fred-ii- that's because he statically sets month=3 in the ajax.. what I don't get is what would make the submit function run when the form is submitted.. Commented Jun 30, 2014 at 18:18
  • 1
    I always get mixed up with Ajax stuff. But when I see a (named) POST variable that doesn't match, it makes me raise a brow. @mishu Commented Jun 30, 2014 at 18:19
  • 1
    You'd need to define onsubmit="submit()" Commented Jun 30, 2014 at 18:25

6 Answers 6

1

You can also use the .load method of jQuery:

function submit()
        {
             var month = 3;
             var formdata = month;
             $('#response').load('showCharts.php?month='+formdata);
        }

Also, you will need to set:

$month = $_REQUEST['month'];
Sign up to request clarification or add additional context in comments.

1 Comment

The URL expands to showCharts.php?month=month=3
1

Another way to do it would be:

$('select').change(function() {
          var formdata = { month: document.getElementsByName('monthSelector')[0].value };
          $('#response').load( 'showCharts.php', formdata);
      });

Comments

0

Try replacing

<FORM ID="form1" METHOD="post" ACTION="">

for

<FORM ID="form1" METHOD="post" ONSUBMIT="submit(); return false;">

It should work.

In the part of jQuery, put this:

function submit()
{
    var month = $('select[name="monthSelector"]').val();
    $.ajax({
        type: 'POST',
        url: 'showCharts.php',
        data:{'month':month},
        success: function(data)
        {
            $("#response").html(data);
        }
    });
}

One more thing: try to improve the HTML code, it will give a better image to your webpage.

Comments

0

Are you sure the submit function is even called? Do you bind the form's submit event at all?

I would do something like $("#form1").submit(submit);

Also, you should return false at the end of submit() to block the default form action (which is refresh the current page I believe)

1 Comment

I see it, but it is, like you said, within the function. What calls the function in the first place?
0

Try to update the variable formdata to make it a json object rather than a string.

<SCRIPT>
            function submit()
            {
                $(function()
                {
                    var month = 3;
                    var formdata = {'month': month}; //change made here
                    $.ajax({
                        type: 'POST',
                        url: 'showCharts.php',
                        data: formdata,
                        success: function(data) {
                            $("#response").html(data);
                        }
                    });
                });
            }
        </SCRIPT>

1 Comment

$.ajax() will convert the object back to a query string anyway before making the request.
0

Your jQuery code should be as follows:

$(function() {
      var month = 3;
      var formdata = { month: month };
      $('#response').load( 'showCharts.php', formdata );

      $('#form1').submit(function( e ) {
          e.preventDefault();
          var formData = { month: this.monthSelector.value };
          $('#response').load( 'showCharts.php', formData);
      });
});

When using the ajax .load() method, here is what you should be aware of:

Request Method

The POST method is used if data is provided as an object; otherwise, GET is assumed.

Therefore, with the above jQuery code, your PHP script need not be changed.

Comments

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.