简体   繁体   中英

HTML PHP show form submit results on same page

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.

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

Another way to do it would be:

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

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.

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)

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM