if (isset($_POST['UpdateBtn'])){ } to stored my input in variable and call the function but when I clink the button name(UpdateBtn) nothing happened

huangapple go评论59阅读模式
英文:

if (isset($_POST['UpdateBtn'])){ } to stored my input in variable and call the function but when I clink the button name(UpdateBtn) nothing happened

问题

Here is the translated part of your provided content:

<?php
if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
    echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">Update list</button>';
}else{
    echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">Add list</button>';
}
?>
if (isset($_POST['UpdateBtn'])){
    $fname = $_POST['fname'];
    $lname = $_POST['lname'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $type = $_POST['type'];
    updatelist($pdo,$id,$fname,$lname,$username,$password,$type);
}
if (isset($_POST['UpdateBtn'])){
    echo "hello world";
}

And here's the translation of your function:

function updatelist($pdo, $id, $fname, $lname, $username, $password, $type){
  try {
      $sql ='UPDATE users set  fname=:fname, lname=:lname,username=:username,password=:password,type=:type WHERE id=:id';
      $statement = $pdo->prepare($sql);
      $statement->bindValue(':id', $id);
      $statement->bindValue(':fname', $fname);
      $statement->bindValue(':lname', $lname);
      $statement->bindValue(':username', $username);
      $statement->bindValue(':password', $password);
      $statement->bindValue(':type', $type);
      $statement->execute();
      echo '<script>alert("Order successfully updated")</script>';
      echo '<script>window.location="page2.php"</script>';
  } catch(Exception $e) {
      echo 'Message: ' . $e->getMessage();
  
      $pdo = null;
      $sql = null;
  }
}

And finally, the translated code for your input form:

<div class="row inputRow">
    <div class="row inputRow">
        <div class="col-2">
            <h5>First name</h5>
            <input type="text" class="form-control" placeholder="fname" name="fname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $fname;} ?>">
        </div>
        <div class="col-2">
            <h5>Last name</h5>
            <input type="text" class="form-control" placeholder="lname" name="lname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $lname;} ?>">
        </div>
        <div class="col-2">
            <h5>Username</h5>
            <input type="text" class="form-control" placeholder="username" name="username" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $username;} ?>">
        </div>
        <div class="col-2">
            <h5>Password</h5>
            <input type="text" class="form-control" placeholder="password" name="password" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $password;} ?>">
        </div>
        <div class="col-6">
            <h5>User type</h5>
            <select class="form-select" aria-label="Default select example" name="type">
                <option value="administrator" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='administrator'){ echo 'selected';} ?>>Administrator</option>
                <option value="customer" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='customer'){ echo 'selected';} ?>>Customer</option>
            </select>
        </div>
    </div>
</div>

I've translated the relevant parts of your PHP code, as requested.

英文:
&lt;?php
if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){
echo &#39;&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary addOrder&quot; name=&quot;UpdateBtn&quot;&gt;Update list&lt;/button&gt;&#39;;
}else{
echo &#39;&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary addOrder&quot; name=&quot;AddOrder&quot;&gt;Add list&lt;/button&gt;&#39;;
}
?&gt;

what am trying to say is when i click the update list button from the code above i expecting if (isset($_POST['UpdateBtn'])) to work but everytime i click nothing happened

if (isset($_POST[&#39;UpdateBtn&#39;])){
$fname = $_POST[&#39;fname&#39;];
$lname = $_POST[&#39;lname&#39;];
$username = $_POST[&#39;username&#39;];
$password = $_POST[&#39;password&#39;];
$type = $_POST[&#39;type&#39;];
updatelist($pdo,$id,$fname,$lname,$username,$password,$type);
}

i try this to see if it echo the hello world but still not working

if (isset($_POST[&#39;UpdateBtn&#39;])){
echo &quot;hello world&quot;;
}

this is my code for my function that save in other php files the rest of the code save in same files

function updatelist($pdo,$id,$fname,$lname,$username,$password,$type){
try {
$sql =&#39;UPDATE users set  fname=:fname, lname=:lname,username=:username,password=:password,type=:type WHERE id=:id&#39;;
$statement = $pdo-&gt;prepare($sql);
$statement-&gt;bindValue(&#39;:id&#39;,$id);
$statement-&gt;bindValue(&#39;:fname&#39;,$fname);
$statement-&gt;bindValue(&#39;:lname&#39;,$lname);
$statement-&gt;bindValue(&#39;:username&#39;,$username);
$statement-&gt;bindValue(&#39;:password&#39;,$password);
$statement-&gt;bindValue(&#39;:type&#39;,$type);
$statement-&gt;execute();
echo &#39;&lt;script&gt;alert(&quot; Order successfully updated&quot;)&lt;/script&gt;&#39;;
echo &#39;&lt;script&gt;window.location=&quot;page2.php&quot;&lt;/script&gt;&#39;;
}catch(Exception $e) {
echo &#39;Message: &#39; .$e-&gt;getMessage();
$pdo = null;
$sql = null;
}
}

this is my code where i can insert a input

 &lt;div class=&quot;row inputRow&quot;&gt;
&lt;div class=&quot;row inputRow&quot;&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; first name&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;fname&quot; name=&quot;fname&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $fname;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; last name&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;lname&quot; name=&quot;lname&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $lname;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; username&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;username&quot; name=&quot;username&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $username;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; password&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;password&quot; name=&quot;password&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $password;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-6&quot;&gt;
&lt;h5&gt; User type&lt;h5&gt;
&lt;select class=&quot;form-select&quot; aria-label=&quot;Default select example&quot; name=&quot;type&quot;&gt;
&lt;option value=&quot;administrator&quot; &lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39; &amp;&amp; $id==&#39;administrator&#39;){ echo &#39;selected&#39;;} ?&gt;&gt;administrator&lt;/option&gt;
&lt;option value=&quot;customer&quot; &lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39; &amp;&amp; $id ==&#39;customer&#39;){ echo &#39;selected&#39;;} ?&gt;&gt;customer&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;/div&gt;

i already ask this to my previous question but i think i didn't properly explain what happened also i use this method in my other project and its work i dont know why in this its not am new in php

答案1

得分: 1

你需要将按钮和其他输入元素放在一个带有 method="post" 的 form 标签内,然后你可以在 $_POST 中接收按钮点击的响应。

更新后的代码看起来像这样:

<form action="" method="post">
    <div class="row inputRow">
        <div class="col-2">
            <h5>名字</h5>
            <input type="text" class="form-control" placeholder="名字" name="名字" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $名字;} ?>">
        </div>
        <div class="col-2">
            <h5>姓氏</h5>
            <input type="text" class="form-control" placeholder="姓氏" name="姓氏" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $姓氏;} ?>">
        </div>
        <div class="col-2">
            <h5>用户名</h5>
            <input type="text" class="form-control" placeholder="用户名" name="用户名" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $用户名;} ?>">
        </div>
        <div class="col-2">
            <h5>密码</h5>
            <input type="text" class="form-control" placeholder="密码" name="密码" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $密码;} ?>">
        </div>
        <div class="col-6">
            <h5>用户类型</h5>
            <select class="form-select" aria-label="默认选择示例" name="类型">
                <option value="管理员" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='管理员'){ echo 'selected';} ?>>管理员</option>
                <option value="客户" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='客户'){ echo 'selected';} ?>>客户</option>
            </select>
        </div>
        <?php
        if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
            echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">更新列表</button>';
        }else{
            echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">添加列表</button>';
        }
        ?>
    </div>
</form>
英文:

You will need to add the buttons and other input elements inside a form tag with method="post", then you can receive the button click response in $_POST.

Updated code will look like this.

&lt;form action=&quot;&quot; method=&quot;post&quot; &gt;            
&lt;div class=&quot;row inputRow&quot;&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; first name&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;fname&quot; name=&quot;fname&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $fname;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; last name&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;lname&quot; name=&quot;lname&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $lname;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; username&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;username&quot; name=&quot;username&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $username;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-2&quot;&gt;
&lt;h5&gt; password&lt;h5&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot;  placeholder=&quot;password&quot; name=&quot;password&quot; value=&quot;&lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){ echo $password;} ?&gt;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;col-6&quot;&gt;
&lt;h5&gt; User type&lt;h5&gt;
&lt;select class=&quot;form-select&quot; aria-label=&quot;Default select example&quot; name=&quot;type&quot;&gt;
&lt;option value=&quot;administrator&quot; &lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39; &amp;&amp; $id==&#39;administrator&#39;){ echo &#39;selected&#39;;} ?&gt;&gt;administrator&lt;/option&gt;
&lt;option value=&quot;customer&quot; &lt;?php if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39; &amp;&amp; $id ==&#39;customer&#39;){ echo &#39;selected&#39;;} ?&gt;&gt;customer&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;?php
if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){
echo &#39;&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary addOrder&quot; name=&quot;UpdateBtn&quot;&gt;Update list&lt;/button&gt;&#39;;
}else{
echo &#39;&lt;button type=&quot;submit&quot; class=&quot;btn btn-primary addOrder&quot; name=&quot;AddOrder&quot;&gt;Add list&lt;/button&gt;&#39;;
}
?&gt;
&lt;/div&gt;
&lt;/form&gt;

huangapple
  • 本文由 发表于 2023年6月13日 16:41:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定