2

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...

3
  • 2
    Why not add action and method attributes to your form and then submit it with .submit()? Commented Mar 26, 2012 at 16:21
  • That sorted it for me, thanks. Commented Mar 26, 2012 at 17:10
  • Great, I'll post it as an answer then. Commented Mar 26, 2012 at 17:11

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

you dont need action if its set in ajax(); make sure to add return false; after the ajax(); The way data is set works, but you dont need to set data manually, as it submits postvars along with that data. You're essentially submitting the data twice this way.
i'm not saying to do both, i'm explaining that without the javascript, you're not really doing a "Redirect" and that it can be achieved without ajax...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.