如何使用PHP使用post方法(Post / Redirect / Get)提交表单后重定向

时间:2013-03-29 12:23:47

标签: php redirect post get

我有以下表格:

<form id="frmAeroportonew" name="frmAeroportonew" method="post" action="<?php echo $editFormAction; ?>">
<table>      
  <tr>
    <td><input name="iata" type="text" id="iata" value="" /></td>
    <td><input name="icao" type="text" id="icao" value="" /></td>
    <td><input name="cidade" type="text" id="cidade" value="" /></td>
    <td><input name="pais" type="text" id="pais" value="" /></td>
    <td><input name="destino" type="checkbox" id="destino" value="" /></td>
    <td><input name="alternativo" type="checkbox" id="alternativo" value="" /></td>
    <td><input name="h24" type="checkbox" id="h24" value="" /></td>
    <td><input name="btnaeroportonew" id="btnaeroportonew" type="submit" value="+" /></td>
  </tr>
</table>
<input type="hidden" name="MM_insert" value="frmAeroportonew">

想法是提交此表单,而不是重定向到另一个页面(teste_redirect.php),将输入(id = iata)值附加到url(teste_redirect.php?iata = input_value)。

PHP代码是在dreamweaver cs6中制作的:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmAeroportonew")) {
  $insertSQL = sprintf("INSERT INTO tblaeroportos (iata, icao, cidade, pais, destino, alternativo, H24) VALUES (%s, %s, %s, %s, %s, %s, %s)",
                   GetSQLValueString($_POST['iata'], "text"),
                   GetSQLValueString($_POST['icao'], "text"),
                   GetSQLValueString($_POST['cidade'], "text"),
                   GetSQLValueString($_POST['pais'], "text"),
                   GetSQLValueString(isset($_POST['destino']) ? "true" : "", "defined","1","0"),
                   GetSQLValueString(isset($_POST['alternativo']) ? "true" : "", "defined","1","0"),
                   GetSQLValueString(isset($_POST['h24']) ? "true" : "", "defined","1","0"));

  mysql_select_db($database_connect, $connect);
  $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());

  $insertGoTo = "teste_redirect.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

我赞成你的所有帮助。三江源

1 个答案:

答案 0 :(得分:3)

将您的insertGoto更改为

$insertGoTo = "teste_redirect.php?iata=".$_POST['iata'];