2016-06-28 101 views
-1

提交後我有一些編碼消息的問題。提交後留言

這裏是代碼:

<?php 
    if(isset($_POST['selected_name'])){ 
     $stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?"); 
     $stmt->bind_param('i', $_POST['selected_name']); 
     $stmt->execute(); 
     $stmt->close(); 
    }else{ 
     echo "<h2>Artikel gelöscht!</h2>"; 
     header ("refresh:3; url=e3_artikel_loeschen.php"); 
    } 
?> 
<form action="" method="POST"> 
    <p>Artikel: 
     <?php 
      $stmt = $mysqli->prepare("SELECT anr, name FROM artikel"); 
      $stmt->execute(); 
      $stmt->bind_result($anr, $name); 
      echo "<select name='selected_name'><br />"; 
      while ($stmt->fetch()) { 
       echo '<option value='.$anr.'>'.$anr.' | '.$name.'</option>'; 
      } 
      echo "</select>"; 
     ?> 
     <input type="submit" name="submit" value="Datensatz löschen" /> 
    </form> 
</body> 
</html> 

我意識到,這樣的消息總是存在,並不會消失。我的願望是編碼,以便讓提交後只顯示消息,然後返回到主頁面。感謝您的幫助。

+0

您想要在提交後顯示哪條消息? – Mad

+0

@Macro檢查我的答案,您的代碼中的邏輯錯誤 – Developer

+0

我試過了。但是隨着代碼的出現,消息將會每隔三秒刷新一次。 – Marco

回答

0

我解決我的問題是這樣的:

我寫的一個主頁之外的代碼:

<?php 
require_once ("db.inc.php"); 
if(isset($_POST['selected_name'])){ 




$stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?"); 
$stmt->bind_param('i', $_POST['selected_name']); 
$stmt->execute(); 
    $stmt->close(); 




if (eregi("[0-9 [a-d]", $_POST['selected_name'])){ 
echo '<meta http- equiv="refresh content="3;url=e3_artikel_loeschen.php">'; 
echo "<h2>test</h2>"; 
} 
} 
?> 

這是主要頁面的代碼:

<!DOCTYPE html> 
<html> 
<meta http-equiv="content-type" content="text/html" /> 
<meta charset="utf-8" /> 
<?php 
require_once("db.inc.php"); 
?> 
<link rel="stylesheet" type="text/css" href="styles2.css" /> 
</head> 
<body> 
<form action="loeschen.php" method="POST"> 
<p>Artikel: 
<?php 
$stmt = $mysqli->prepare("SELECT anr, name FROM artikel"); 
$stmt->execute(); 
$stmt->bind_result($anr, $name); 
echo "<select name='selected_name'><br />"; 
while ($stmt->fetch()) { 
echo '<option value='.$anr.'>'.$anr.' | '.$name.'</option>'; 

} 

echo "</select>"; 

?> 
<input type="submit" name="submit" value="Datensatz löschen" /> 
</form> 
</body> 
</html> 

希望這可以幫助也爲他人。

0

您正在嘗試在發送標題後發送標題(通過echo發送消息)。把header行前echo,它應該工作。請注意:如果此代碼是您計劃保留的內容多於完全沒有的內容,請了解如何從視圖中分離模型和控制器。學習和使用PSR-1和PSR-2也不會傷害任何一方。

+0

此外,在這種情況下,如果我在回聲之前放置標題行,我有同樣的問題。提交後出現消息,但在此之後,消息仍然出現在主頁面,並且每三秒刷新一次不停止 – Marco

0
echo '<script> 
alert("You have selected '.$language.'"); 
window.location = "'.$SITE_URL.'index.php?view=view_service." 
</script>'; 
0
<?php 
$con=mysqli_connect("localhost","root","","db"); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

// escape variables for security 
$name = mysqli_real_escape_string($con, $_POST['name']); 
$email = mysqli_real_escape_string($con, $_POST['email']); 
$password = mysqli_real_escape_string($con, $_POST['password']); 
$phoneno = mysqli_real_escape_string($con, $_POST['phoneno']); 
$status = mysqli_real_escape_string($con, $_POST['status']); 

$sql = "SELECT * FROM register WHERE email='".$email."' AND status='1'"; 
$result = mysqli_query($con, $sql); 
if (mysqli_num_rows($result) > 0) 
    { 
     $errormsg="email already exist"; 
    } 
else{ 
$sql="INSERT INTO register (name,email,password,phoneno,status) VALUES ('$name','$email','$password','$phoneno','1')"; 
     if (!mysqli_query($con,$sql)) 
      { 
       die('Error: ' . mysqli_error($con)); 
      } 
     exit; 
mysqli_close($con); 
} 
?> 
<form role="form" action="" autocomplete="off" method="POST"> 

    <div class="input-group"> 
     <span class="input-group-addon"><i class="fa fa-user"></i></span> 
      <input type="name" class="form-control" name="name" placeholder="Company Name" required> 
    </div> 
    <span class="help-block"></span> 

    <div class="input-group"> 
     <span class="input-group-addon"><i class="fa fa-user"></i></span> 
      <input type="email" class="form-control" name="email" placeholder="email address" required> 
    </div> 
    <?php echo $errormsg;?></span> 

    <div class="input-group"> 
     <span class="input-group-addon"><i class="fa fa-lock"></i></span> 
      <input type="password" class="form-control" name="password" placeholder="Password" required> 
    </div> 
    <span class="help-block"></span> 

    <div class="input-group"> 
     <span class="input-group-addon"><i class="fa fa-user"></i></span> 
      <input type="tel" id="tel" class="form-control" name="phoneno" pattern="\d{10}" placeholder="Please enter a ten digit phone number" required /> 
    </div> 
    <span class="help-block"></span> 
    <input class="form-control" value="" type="hidden" name="status"> 

    <button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign Up</button> 

</form> 
0

在這種情況下,你可以使用JavaScript重定向與2延遲第二:

echo "<h2>Artikel gelöscht!</h2>"; 

echo '<script>setTimeout(function() { 
    window.location.href = "e3_artikel_loeschen.php""; 
}, 2000); </script>'; 
0

移動

echo "<h2>Artikel gelöscht!</h2>"; 
header ("refresh:3; url=e3_artikel_loeschen.php"); 

if(isset($_POST['selected_name'])){} 

,並重新排序,以

header ("refresh:3; url=e3_artikel_loeschen.php"); 
echo "<h2>Artikel gelöscht!</h2>"; 

UPDATE:試試這個

<?php 
if($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['selected_name'])) { 
     $stmt = $mysqli->prepare("DELETE FROM artikel WHERE anr=?"); 
     $stmt->bind_param('i', $_POST['selected_name']); 
     $stmt->execute(); 
     $stmt->close(); 
     header ("refresh:3; url=e3_artikel_loeschen.php"); 
     echo "<h2>Artikel gelöscht!</h2>"; 
} 
?> 

PHPFIDDLE EXAMPLE

+0

因此,我不需要編碼} else {? – Marco

+0

我做到了,但仍然是同樣的問題。頁面每三秒刷新一次,消​​息不會消失。 – Marco

+0

@Marco是這個表格代碼與主頁在同一頁上? –