如何在javascript和php mysql中将表单操作修复为不同的页面问题,

i want to check user browser type and click form submit button to action to different page according to browser type, but when i use PHP while loop, the <form xxxxxxx onSubmit="javascript function()"> can't run, just go link/a.php with two browser. If I remove the while loop, it can run successfully such as Chrome Browser to go a.php and IE Browser to go b.php. How to fix it? the code is below. The first step is import the check broswer type's javascript code from Quirksmode

Then this is a while loop version.

<script>
   function redirect(){
      if(BrowserDetect.browser == 'Chrome'){
         document.getElementById("albumlist").setAttribute("action", "link/a.php");
      }
      if(BrowserDetect.browser == 'Explorer'){
         document.getElementById("albumlist").setAttribute("action", "link/b.php");
      }
   }
</script>

In the <body></body>.

<?php

$result = mysql_query("SELECT * FROM album GROUP BY folderName ORDER BY id desc");

while($data = mysql_fetch_array($result)){

   $folderName = $data['folderName'];
   ...
   ...

?>

   <form name="albumlist" id="albumlist" method="post" action="link/a.php" target="_blank" onSubmit="redirect();">
      <input type="hidden" name="folderName" id="folderName" value="<?php echo $folderName; ?>" />
      <input type="submit" id="submitfolderlist" name="submitfolderlist" value="Submit" />
   </form>

<?php

}

?>

Then a without while loop version just remove all about <?php while loop code ?>

Change the JS to:

<script>
   function redirect(form){
      if(BrowserDetect.browser == 'Chrome'){
         form.setAttribute("action", "link/a.php");
      }
      if(BrowserDetect.browser == 'Explorer'){
         form.setAttribute("action", "link/b.php");
      }
   }
</script>

Then change your PHP to:

   <form name="albumlist" method="post" action="link/a.php" target="_blank" onSubmit="redirect(this);">
      <input type="hidden" name="folderName" id="folderName" value="<?php echo $folderName; ?>" />
      <input type="submit" name="submitfolderlist" value="Submit" />
   </form>

This gets rid of the duplicate IDs on the form and submit button.

I would just detect the browser on this current page with PHP....and change the action link accordingly. That way you completely remove the need for javascript.

Something like this at the top of your page......

$ua = $_SERVER["HTTP_USER_AGENT"];
/* ==== Detect the UA ==== */
$chrome = strpos($ua, 'Chrome') ? true : false; 

// Internet Exlporer
$msie = strpos($ua, 'MSIE') ? true : false; 
// All Internet Explorer

if($chrome){ $link = "link/a.php";}
elseif($msie){$link = "link/b.php";}

Then in your form just echo out the $link variable for the action...

<form name="albumlist" id="albumlist" method="post" action="<?echo $link;?>" target=_blank> 

PS Totally untested...Im just giving you an idea...