Adam Waite
Adam Waite

Reputation: 18855

jQuery POST values to PHP script

I want to post some values from a simple HTML form, validate them with an ajax call and if successful submit to a PHP script and redirect to that script. I have got the ajax part set up, just not sure how to post and redirect (as if you would on a standard form submit without ajax).

Here's what I have:

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 Call:

 <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 to handle 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';
}

?>

Preferably I could achieve this using jQuery. I'm sure it's extremely simple but I'm not sure how to do it?!

It's worth mentioning that this is all set up correctly. I have used PHP includes.

Thanks in advance...

Upvotes: 2

Views: 4167

Answers (2)

j08691
j08691

Reputation: 208002

Why not add action and method attributes to your form and then submit it with .submit()?

Upvotes: 2

Kristian
Kristian

Reputation: 21840

With plain html / php it is not even really a true redirect, its just the url value in "action" that can be found in the form element

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

If you're doing it with ajax (jquery), you'd say:

$.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';
    }
  });

Upvotes: 0

Related Questions