1

i need a help

in php i have a select box like this

<?php
$perpage  = 5; 
$total    = 128; 
$num      = ceil( $total / $perpage );

$i = 1;

echo "<FORM NAME=\"form1\">";
echo "Select <SELECT NAME=\"select\" onChange=\"goto(this.form)\" SIZE=\"1\" >";
echo "<OPTION VALUE=\"\">----Select Page----";

for($i = 1; $i <= $num; $i++)
{
  $sel  = $i;
  $goto = ($i - 1) * $perpage;

  if($goto == 0) { $goto = ''; }

  echo "<OPTION VALUE=\"http://localhost/CI_doctrine/blog/filmnews/" . $goto . "\">" . $i . "";
}

echo "</SELECT> Page";
echo "</FORM>";
?>

javascript code is here

function goto(form) {
  var index = form.select.selectedIndex;

  if (form.select.options[index].value != "0") {
    window.location = form.select.options[index].value;
    form.select.selected = form.select.options[index].value;
  }
}

the code is working fine but i want to change the selected option to set the selected number after the page redirection but here iam getting the "select page" as the selected option

any help appreciated. thank you from your friend.

1
  • You'll have to do that with PHP, not JavaScript. Commented Oct 23, 2011 at 5:59

1 Answer 1

1

Once you redirect, that page is unloaded and a new page loaded (even if it has the same items). When the new page loads, you want to do:

window.onload = function() {
    var i, options = form.select.options, curUrl = window.location.href;
    for (i = 0; i < options.length; i++) {
        if (options[i].value == curUrl) {
            form.select.selectedIndex= i;
            break;
        }
    }
}

This will select the current URL. Make sure the URLs in the select options are full URLs (including http://). Also, window.onload is the DOM1 way. You should probably use a real library to deal with this.

Alternatively, you can also select the right input in PHP using the same basic approach.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.