PHP GET方法空间错误

Cha*_*ray 1 html php get

<?php

$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];

echo "<form method=\"post\" action=mainpage.php?subject_id=".$current_subject."&note_id=".$current_content.">";

?> 
  <input type='text' name='list_item' value=''>
  <input type='submit' name="new_item" value="New Item">   
</form>
Run Code Online (Sandbox Code Playgroud)

问题是,当其中一个GET变量是两个单词时,链接就不会这样写.例如,如果$current_subject="Advanced Chemistry"$current_content="Valence Electrons"链接将出现如下:

<form method=?"post" action=?"mainpage.php?subject_id=Advanced" chemistry&note_id=?"Valence" electrons>?
Run Code Online (Sandbox Code Playgroud)

Jos*_*ith 5

你需要urlencode()像这样的变量:

<?php

$current_subject = $_GET['subject_id'];
$current_content = $_GET['note_id'];

$subject = urlencode($current_subject);
$content = urlencode($current_content);

$action = "mainpage.php?subject_id=" . $subject . "&note_id=" . $content;

?>

<form method="post" action="<?php echo $action; ?>">
  <input type="text" name="list_item" value="">
  <input type="submit" name="new_item" value="New Item">   
</form>
Run Code Online (Sandbox Code Playgroud)

此外,您应该养成验证数据的习惯.您可能想要检查它们是否为整数.

  • 你错放了urlencode:`echo urlencode("mainpage.php?subject_id =1234łóść&note_id = absd saas")`给`mainpage.php%3Fsubject_id%3D1234 +%C5%82%C3%B3%C5%9B%C4%87 %26note_id%3Dabsd + saas` (2认同)