我正在使用php开发一个应用程序,我会在一个页面中添加2个按钮php,我将每个Bottons的名称传递给函数(POST),就像这样(isset($ _ POST ['action']))但是我不能帮你做到这一点
<?php
$host = "localhost";
$user = "root";
$db = "reseau";
$con = @mysql_connect('localhost', 'root', '');
$select = @mysql_select_db ('reseau') ;
if (isset($_POST['action'])) {
if(isset($_POST['nom'])) $nom = htmlspecialchars(pg_escape_string($_POST['nom']));
else $nom="";
if(isset($_POST['prenom'])) $prenom = htmlspecialchars(pg_escape_string($_POST['prenom']));
else $prenom="";
if(isset($_POST['email'])) $email = $email = htmlspecialchars(pg_escape_string($_POST['email']));
else $email="";
if(isset($_POST['password'])) $password = htmlspecialchars(pg_escape_string($_POST['password']));
else $password="";
if(isset($_POST['naissance'])) $naissance = htmlspecialchars(pg_escape_string($_POST['naissance']));
else $naissance="";
if(isset($_POST['sexe'])) $sexe = htmlspecialchars(pg_escape_string($_POST['sexe']));
else $sexe="";
$query = "INSERT INTO usager(nom,prenom,email,password,naissance,sexe) VALUES ('" . $nom . "', '" . $prenom . "', '" . $email . "', '" . $password . "', '" . $naissance . "', '" . $sexe . "')";
$result = mysql_query($query);
if (!$result) {
$errormessage = mysql_error();
echo "Error with query: " . $errormessage;
exit();
}
}
else if(isset($_POST['enter'])){
if(empty($_POST['nom']) === false && empty($_POST['password']) === false){
$nom = htmlspecialchars(pg_escape_string(trim($_POST['nom'])));
$password = htmlspecialchars(pg_escape_string(trim($_POST['password'])));
$query = mysql_query("SELECT * FROM usager WHERE nom = '$nom' AND password = '$password'")or die(mysql_error());
$count = mysql_num_rows($query);
if($count == 0){
$output= "combinaison nom d'utilisateur/mot de passe incorrecte. réessayez";
}else{
session_start();
$_SESSION['nom']=htmlspecialchars(pg_escape_string(trim($_POST['nom'])));
$_SESSION['password']=htmlspecialchars(pg_escape_string(trim($_POST['password'])));
header("Location: index1.php");
}
}else{
$output= 'veuillez saisir tout les champs';
}
}
?>
<!DOCemail html>
<html>
<body style="background-image:url(6a00e554acca9b8834014e86765de6970d.jpg);
background-attachment : fixed;
background-position : 50% 50%;
background-size : cover;
">
<form name="nl" action="" method="POST">
<fieldset style="width: 370px; margin-top: 100px;">
<legend style= "left: 120px;">Creer un compte</legend>
<p><label for="text">Nom</label><input required placeholder="votre nom" title="Ce texte ne doit comprendre que des lettres" id="text" email="text" name="nom" style="height: 31px; margin-left: 25px;"/></p>
<p><label for="email">Prenom</label><input required placeholder="votre prenom " id="email" type="text" name="prenom"style="height: 31px; margin-left: 25px;" /></p>
<p><label for="password">Email</label><input required placeholder="votre email " id="url" type="text" name="email" style="height: 31px; margin-left: 25px;"/></p>
<p><label for="url">Mot de passe</label><input required placeholder="votre mot de passe " id="url"type="password" name="password" style="height: 31px; margin-left: 25px;"/></p>
<p><label for="url" style="width: 147px;">Date de naissance</label><input required id="birthdate" name="naissance" type="date" style="height: 31px;"/></p>
<p><label style="
width: 90px;
">Sexe</label><a>homme</a>
<input type="radio" name="sexe" value="male"
checked="checked" style="
width: 20px;
"/>
<a>femme</a>
<input type="radio" value="women" name="sexe" style="
width: 20px;
"/></p>
</fieldset>
<button type='submit' name='action' class="button" value="action" style="
padding-left: 18px;
padding-right: 18px;
padding-top: 13px;
padding-bottom: 13px;
margin-top: 0px;
border-top-width: 0px;
margin-right: 950px;
">Inscription</button>
<div id="main" class="container" style="left: 1150px;top: 270px;">
<fieldset>
<legend style="
margin-left: 65px;
">Login In</legend>
<p class="login-username">
<label for="nom">Nom:</label> <input type="text" name="nom" id="user_login" class="input" value="" size="20"
placeholder="Username" style="
height: 36px;
">
</p>
<p class="login-password">
<label for="password">Mot de passe</label> <input type="password" name="password" id="user_pass" class="input" value="" size="20" placeholder="Password" style="
height: 36px;
">
</p>
<p class="login-remember">
<label><input name="rememberme" type="checkbox" id="rememberme" value="forever"> Remember Me</label>
</p>
<p class="login-submit">
<input type='submit' name='enter' id="wp-submit" class="button-primary" value="Sign In" style="
width: 110px;
height: 36px;
"/>
</p>
</fieldset>
</div>
</form>
感谢任何帮助。
答案 0 :(得分:1)
创建两个提交按钮。为具有不同值的两个提交输入指定相同的名称。
<input type="submit" name="btn_name" value="First Button"/>
<input type="submit" name="btn_name" value="Second Button"/>
并检查它,
if(isset($_POST) && isset($_POST['btn_name']))
{
if($_POST['btn_name'] == "First Button")
{
// first button clicked
}
else if($_POST['btn_name'] == "Second Button")
{
// second button clicked
}
}
答案 1 :(得分:0)
请在两个按钮中添加名称标签,然后使用 isset()功能进行检查
<input type="submit" name="btn1" value="btn1" />
<input type="submit" name="btn2" value="btn2" />
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
if (isset($_POST['btn1'])) {
// btn1
} else {
//btn2
}
}