【问题标题】:jQuery POST values to PHP scriptjQuery POST 值到 PHP 脚本
【发布时间】:2012-04-10 04:05:53
【问题描述】:

我想从一个简单的 HTML 表单中发布一些值,使用 ajax 调用验证它们,如果成功提交到 PHP 脚本并重定向到该脚本。我已经设置了 ajax 部分,只是不确定如何发布和重定向(就像你会在没有 ajax 的标准表单上提交一样)。

这是我所拥有的:

HTML:

<div id=audiencesearch>

    <h1>Audience Search</h1>

<form id="audiencesearchform">

<p>Passion Point</p>
<select id="passionselect">
<option selected="selected">Please select</option>
    <option>3D</option>
    <option>Music</option>
<option>Playstation</option>
    </select>

<p>Gender Bias</p>
<select id="genderselect">
<option selected="selected">Please select</option>
    <option>Male</option>
    <option>Female</option>
    </select>

<p>Sort Group By Age Range</p>
<select id="ageselect">
<option selected="selected">Please select</option>
<option>Under 21</option>
    <option>21 - 30</option>
    <option>31 - 40</option>
<option>41 - 50</option>
    </select>

<br/>
<br/>

<input onClick="ajaxaudiencesearch()" class="submitaudiencesearch" value="Search" type="button"/>

Ajax 调用:

 <script type="text/javascript">

function ajaxaudiencesearch(){

    var passionpoint = $("select#passionselect").val();  
    var genderbias = $("select#genderselect").val();  
    var agerange = $("select#ageselect").val();
    var passedstring = 'passion='+ passionpoint + '&gender=' + genderbias + '&age=' + agerange;

    $.ajax({
        type: "POST",
        url: "processaudiencesearch.php",
        data: passedstring,
        success:function(retval){       
            if (retval == 'oktoprocess'){

                audiencesearchprocess();

            } else {
                audiencesearcherror();
            }

        }
    })
}

function audiencesearcherror(){

    $('#audienceerror').html('GOTTA SELECT EM ALL');

}

function audiencesearchprocess(){

    window.location.href = "searchresults.php";

//THIS IS WHERE I WANT TO POST AND MOVE TO SEARCHRESULTS.PHP

}

</script>

PHP 处理 Ajax:

    <?php

include('sonymysqlconnect.php'); 
session_start();

$nullselection = "Please select";

//get the posted values
$postedpassion = ($_POST['passion']);
$postedgender = ($_POST['gender']);
$postedage = ($_POST['age']);


if (($postedpassion != $nullselection ) && ($postedgender != $nullselection ) && ($postedage != $nullselection)){
    echo 'oktoprocess';
} else {
    echo 'error';
}

?>

最好我可以使用 jQuery 来实现这一点。我敢肯定这非常简单,但我不知道该怎么做?!

值得一提的是,这一切都设置正确。我用过 PHP 包含。

提前谢谢...

【问题讨论】:

  • 为什么不在表单中添加动作和方法属性,然后使用 .submit() 提交?
  • 帮我整理好了,谢谢。
  • 太好了,我会把它作为答案发布。

标签: php javascript jquery ajax post


【解决方案1】:

为什么不在表单中添加操作和方法属性,然后使用 .submit() 提交?

【讨论】:

    【解决方案2】:

    对于普通的 html / php,它甚至不是真正的重定向,它只是“action”中的 url 值,可以在表单元素中找到

    <form action="/someUrl/someAction" method="POST">
    ...
    </form>
    

    如果你用 ajax (jquery) 来做,你会说:

    $.ajax({
      url: '/someUrl/someAction',
      type: 'POST',
      data: {
        argName1: 'argVal1',
        argName2: 'argVal2'
      },
      success: function( response ) {
        // keep in mind that response is usually in json...
        // which means you'd be accessing
        // the data in object notation: response.data.someVal
        if(response == 'whatYouWanted') {
           //do something... like redirect?
           window.location = '/new/page';
        }
      });
    

    【讨论】:

    • 如果它在ajax(); 中设置,则不需要操作确保在ajax(); 之后添加return false; 数据设置方式有效,但您不需要手动设置数据,因为它提交postvars 以及该数据。您实际上是通过这种方式提交了两次数据。
    • 我不是说两者都做,我是在解释没有 javascript,你并没有真正做一个“重定向”,它可以在没有 ajax 的情况下实现......
    猜你喜欢
    • 2014-09-03
    • 2017-04-23
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多