使用HTML / JQuery / PHP提交表单和返回值

时间:2022-04-13 09:37:02

need your help for this ... My homepage have 3 divs, #Header, #Content, #Footer. All the other pages are being opened inside the #Content div. In one of those pages I have a form with two select lists and one submit button. Just want to click the button and then return another page into the #Content div, showing the values that I select before. Like this:

这事需要你的帮助……我的主页有三个div, #Header, #Content, #Footer。所有其他页面都在#Content div中打开。在其中一个页面中,我有一个包含两个选择列表和一个提交按钮的表单。只需单击按钮,然后返回另一个页面到#Content div,显示我之前选择的值。是这样的:

The origin is: 1
The destiny is: 1

起源是:1命运是:1

But this code returns the following ...

但是这个代码返回以下内容……

Notice: Undefined variable: origin in ...
Notice: Undefined variable: destiny in ...

Note: This is working if I don't open the page inside the #Content div

注意:如果我不打开#Content div中的页面,这就可以工作了

my Html:

我的Html:

<form id="myform" name="myform" action="values.php" method="POST">
    <select id="origin" name="origin">
        <option value="0" selected>-- Select Origin --</option>
        <option value="1">Portugal</option></select>
    <select id="destiny" name="destiny">
        <option value="0" selected>-- Select Destiny --</option>
        <option value="1">Lisboa</option></select>
    <input id="btSubmit" name="btSubmit" type="submit" value="search!">
</form>

my Function:

我的函数:

$(document).ready(function(){
    $('#btSubmit').click(function(e) {
        e.preventDefault();
        var url = $('#myform').attr('action');
        var method = $('#myform').attr('method');
    $.ajax({
        type: method,
        url: url,
        data: $('#myform').serialize(),
        success: $('#content').load(url)
        });
    });
});

my values.php page:

我的价值观。php页面:

<?php
    if(isset($_POST['origin']) || isset($_POST['destiny'])) 
    {
        $origin = $_POST['origin'];
        $destiny = $_POST['destiny'];
    }
    echo 'The origin is:' . $origin . '<br>';
    echo 'The destiny is:' . $destiny;
?>

3 个解决方案

#1


3  

You should not call load again - you have already called it essentially with $.ajax and received the results. So you need just display them in the content:

你不应该再调用load,你已经用$来调用它了。ajax并接收结果。所以你需要在内容中显示它们

success: function (data) {
    $('#content').html(data);
}

#2


0  

You should use success callback function correctly. Accept response in callback method and set it in your div

您应该正确使用成功回调函数。接受回调方法中的响应并将其设置为div

success: function (data) {
    $('#content').html(data);
}

Additionally, You should perform your operation with form submit event.

此外,您应该使用表单提交事件执行操作。

$('form#myform').on('submit', function (e) {

instead of

而不是

$('#btSubmit').click(function(e) {

#3


0  

As Andrei mentioned you have to use

正如安德烈所说,你必须使用

success: function (data) {
    $('#content').html(data);
}

because calling success: $('#content').load(url) triggers a new GET request. When GET request reaches php code $_POST is not set and your variables are not initialized so you get the message from php:

因为调用success: $('#content').load(url)会触发一个新的GET请求。当GET请求到达php代码时,$_POST没有设置,您的变量没有初始化,因此您从php获得消息:

Notice: Undefined variable: origin in 

#1


3  

You should not call load again - you have already called it essentially with $.ajax and received the results. So you need just display them in the content:

你不应该再调用load,你已经用$来调用它了。ajax并接收结果。所以你需要在内容中显示它们

success: function (data) {
    $('#content').html(data);
}

#2


0  

You should use success callback function correctly. Accept response in callback method and set it in your div

您应该正确使用成功回调函数。接受回调方法中的响应并将其设置为div

success: function (data) {
    $('#content').html(data);
}

Additionally, You should perform your operation with form submit event.

此外,您应该使用表单提交事件执行操作。

$('form#myform').on('submit', function (e) {

instead of

而不是

$('#btSubmit').click(function(e) {

#3


0  

As Andrei mentioned you have to use

正如安德烈所说,你必须使用

success: function (data) {
    $('#content').html(data);
}

because calling success: $('#content').load(url) triggers a new GET request. When GET request reaches php code $_POST is not set and your variables are not initialized so you get the message from php:

因为调用success: $('#content').load(url)会触发一个新的GET请求。当GET请求到达php代码时,$_POST没有设置,您的变量没有初始化,因此您从php获得消息:

Notice: Undefined variable: origin in