search

Home  >  Q&A  >  body text

jquery form submission problem

index.html的代码:

<html>

<head>

<meta charset="utf-8">

<script src="jquery-1.12.4.min.js"></script>

<script type="text/javascript">

 

    $(function () {

        $("#btn").click(function () {

            $.ajax({ 

                    type:"post", 

                    url:"form.php", 

                    data:{

                        bookname:$("#bookname").val(),

                        press:$("#press").val()

                    },

                    async: true, 

                    success: function(msg) { 

                        alert("提交成功!" msg); 

                    } 

                }); 

   

        });

    });

   

</script>

</head>

<body>

    <div style="text-align: center; margin-top: 50px;">

        <form id="form1">

            图书名:<input type="text" id="bookname" /><br>

                                                                                                                                                                                                   type="button" value="Submit" style="margin-top: 27px;" />

#</body>

</html>

##form.php code:

<? php

$a = $_POST["bookname"];

$b = $_POST["press"];

echo $a;

echo $b;

?>

Run index.html, enter in the form and click submit:

Open the form.php page, but it is blank, and echo cannot output the content.

1.png

The effect you want to achieve is:

1. Submit the form through ajax without jumping to the page. 2.png

2. The php page can receive the form data submitted by ajax, and output the form data through echo and display it on the php page.

Currently, the php page can receive form data submitted by ajax, because the data can be returned to the parameter msg. However, when the form data is output through echo and displayed on the php page, it is blank. What went wrong?

hanghaigoodhanghaigood1230 days ago1305

reply all(3)I'll reply

  • 流年

    流年2021-07-20 18:56:05

    The double quotes in the background become single quotes

    reply
    0
  • hanghaigood

    $_POST['bookname'], even if the double quotes are changed into single quotes, it won't work either.

    hanghaigood · 2021-07-21 14:58:35
  • 流年

    流年2021-07-20 18:55:06

    Look inside the controller

    reply
    0
  • Cancelreply