开发者

onchange event to handle URL redirect and form submit()

I have the form submission because it is a simple

onchange="submit();"

                            <div class="cities">Select City <form method="post" action="<?php echo $PHP_SELF;?>" style="float:right;"><select name="city_select" id="city_select" onchange="submit();"><option&开发者_开发百科gt;-----</option>
                        <option value="Beijing" <?php if($_SESSION['city_selected'] == 'Beijing') { echo 'selected=selected';} else { echo ''; }?> >Beijing</option>
                        <option value="Shanghai" <?php if($_SESSION['city_selected'] == 'Shanghai') { echo 'selected=selected';} else { echo ''; }?> >Shanghai</option>
                        <option value="Guangzhou" <?php if($_SESSION['city_selected'] == 'Guangzhou') { echo 'selected=selected';} else { echo ''; }?> >Guangzhou</option>
                        <option value="Manila" <?php if($_SESSION['city_selected'] == 'Manila') { echo 'selected=selected';} else { echo ''; }?> >Manila</option>
                        <option value="HongKong" <?php if($_SESSION['city_selected'] == 'HongKong') { echo 'selected=selected';} else { echo ''; }?> >Hong Kong</option>
                        <option value="Tokyo" <?php if($_SESSION['city_selected'] == 'Tokyo') { echo 'selected=selected';} else { echo ''; }?> >Tokyo</option>
                        <option value="Seoul" <?php if($_SESSION['city_selected'] == 'Seoul') { echo 'selected=selected';} else { echo ''; }?> >Seoul</option>
                        <option value="Taipai" <?php if($_SESSION['city_selected'] == 'Taipai') { echo 'selected=selected';} else { echo ''; }?> >Taipai</option>
                        </select></form>
                        <div style="clear:both;"></div>
                        </div>

I'm trying to submit the form and also redirect to a url. I was wondering if this was possible. I've tried implementing a jquery $.change event but it didn't fire before the form submit()

if anyone could help, that would be fantastic. Thanks!


Just do it in php by: header('Location: ' . the_url_to_redirect);


where is your submit function?

Can you try like this

<select name="city_select" id="city_select" onchange="this.form.submit();">

It should work


You have not shown what your submit function does, but if it does a normal form submission (not AJAX), then you will need to redirect at a later time.

Normally, you would have your form submit normally, and then when the next page loads, that page would set a 'Location' header telling the browser to redirect.

For example, in whatever PHP code runs when the form submits, you will need to redirect by doing this:

header('Location', 'http://mysite.com/page-to-redirect-to.php');


All you need is "Jquery Form Plugin"

http://jquery.malsup.com/form/#ajaxForm

With this plug-in the form submits to the 'action' address without leaving the page. When it's done you can redirect with the callback function.

<script>
    $(document).ready(function(){
        $('#FORM_ID').ajaxForm(function(data) {
            window.location="http://www.REDIRECT_URL.com/"; 
        });                          
    });
</script>

If you need this onchange you can use this one :

<script>
    $(document).ready(function(){   

        $('#FORM_ID').ajaxForm(function(data) {
            window.location="http://www.REDIRECT_URL.com/"; 
        });  

        $('#city_select').change(function(){
            $('#FORM_ID').trigger('submit');
        });

    });
</script>


if(isset($_POST['city_select'])) {

$_SESSION['city_selected'] = $_POST['city_select'];

if($_POST['city_select'] == "Beijing") {
    header("Location: /city-beijing/");
}
if($_POST['city_select'] == "Shanghai") {
    header("Location: /city-shanghai/");
}
if($_POST['city_select'] == "Guangzhou") {
    header("Location: /city-guangzhou/");
}
if($_POST['city_select'] == "Manila") {
    header("Location: /city-manila/");
}
if($_POST['city_select'] == "HongKong") {
    header("Location: /city-hong-kong/");
}
if($_POST['city_select'] == "Tokyo") {
    header("Location: /city-tokyo/");
}
if($_POST['city_select'] == "Seoul") {
    header("Location: /city-seoul/");
}
if($_POST['city_select'] == "Taipai") {
    header("Location: /city-taipai/");
}

else{

}

}

this worked for me :), cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜