jquery form submission problem
hanghaigood
hanghaigood 2021-07-19 15:25:56
0
3
1284

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?

hanghaigood
hanghaigood

reply all(2)
流年

The double quotes in the background become single quotes

  • reply $_POST['bookname'], even if the double quotes are changed into single quotes, it won't work either.
    hanghaigood author 2021-07-21 14:58:35
流年

Look inside the controller

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template