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评论101阅读模式
英文:

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:

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

And here's the translation of your function:

  1. function updatelist($pdo, $id, $fname, $lname, $username, $password, $type){
  2. try {
  3. $sql ='UPDATE users set fname=:fname, lname=:lname,username=:username,password=:password,type=:type WHERE id=:id';
  4. $statement = $pdo->prepare($sql);
  5. $statement->bindValue(':id', $id);
  6. $statement->bindValue(':fname', $fname);
  7. $statement->bindValue(':lname', $lname);
  8. $statement->bindValue(':username', $username);
  9. $statement->bindValue(':password', $password);
  10. $statement->bindValue(':type', $type);
  11. $statement->execute();
  12. echo '<script>alert("Order successfully updated")</script>';
  13. echo '<script>window.location="page2.php"</script>';
  14. } catch(Exception $e) {
  15. echo 'Message: ' . $e->getMessage();
  16. $pdo = null;
  17. $sql = null;
  18. }
  19. }

And finally, the translated code for your input form:

  1. <div class="row inputRow">
  2. <div class="row inputRow">
  3. <div class="col-2">
  4. <h5>First name</h5>
  5. <input type="text" class="form-control" placeholder="fname" name="fname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $fname;} ?>">
  6. </div>
  7. <div class="col-2">
  8. <h5>Last name</h5>
  9. <input type="text" class="form-control" placeholder="lname" name="lname" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $lname;} ?>">
  10. </div>
  11. <div class="col-2">
  12. <h5>Username</h5>
  13. <input type="text" class="form-control" placeholder="username" name="username" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $username;} ?>">
  14. </div>
  15. <div class="col-2">
  16. <h5>Password</h5>
  17. <input type="text" class="form-control" placeholder="password" name="password" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $password;} ?>">
  18. </div>
  19. <div class="col-6">
  20. <h5>User type</h5>
  21. <select class="form-select" aria-label="Default select example" name="type">
  22. <option value="administrator" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='administrator'){ echo 'selected';} ?>>Administrator</option>
  23. <option value="customer" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='customer'){ echo 'selected';} ?>>Customer</option>
  24. </select>
  25. </div>
  26. </div>
  27. </div>

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

英文:
  1. &lt;?php
  2. if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){
  3. 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;;
  4. }else{
  5. 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;;
  6. }
  7. ?&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

  1. if (isset($_POST[&#39;UpdateBtn&#39;])){
  2. $fname = $_POST[&#39;fname&#39;];
  3. $lname = $_POST[&#39;lname&#39;];
  4. $username = $_POST[&#39;username&#39;];
  5. $password = $_POST[&#39;password&#39;];
  6. $type = $_POST[&#39;type&#39;];
  7. updatelist($pdo,$id,$fname,$lname,$username,$password,$type);
  8. }

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

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

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

  1. function updatelist($pdo,$id,$fname,$lname,$username,$password,$type){
  2. try {
  3. $sql =&#39;UPDATE users set fname=:fname, lname=:lname,username=:username,password=:password,type=:type WHERE id=:id&#39;;
  4. $statement = $pdo-&gt;prepare($sql);
  5. $statement-&gt;bindValue(&#39;:id&#39;,$id);
  6. $statement-&gt;bindValue(&#39;:fname&#39;,$fname);
  7. $statement-&gt;bindValue(&#39;:lname&#39;,$lname);
  8. $statement-&gt;bindValue(&#39;:username&#39;,$username);
  9. $statement-&gt;bindValue(&#39;:password&#39;,$password);
  10. $statement-&gt;bindValue(&#39;:type&#39;,$type);
  11. $statement-&gt;execute();
  12. echo &#39;&lt;script&gt;alert(&quot; Order successfully updated&quot;)&lt;/script&gt;&#39;;
  13. echo &#39;&lt;script&gt;window.location=&quot;page2.php&quot;&lt;/script&gt;&#39;;
  14. }catch(Exception $e) {
  15. echo &#39;Message: &#39; .$e-&gt;getMessage();
  16. $pdo = null;
  17. $sql = null;
  18. }
  19. }

this is my code where i can insert a input

  1. &lt;div class=&quot;row inputRow&quot;&gt;
  2. &lt;div class=&quot;row inputRow&quot;&gt;
  3. &lt;div class=&quot;col-2&quot;&gt;
  4. &lt;h5&gt; first name&lt;h5&gt;
  5. &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;
  6. &lt;/div&gt;
  7. &lt;div class=&quot;col-2&quot;&gt;
  8. &lt;h5&gt; last name&lt;h5&gt;
  9. &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;
  10. &lt;/div&gt;
  11. &lt;div class=&quot;col-2&quot;&gt;
  12. &lt;h5&gt; username&lt;h5&gt;
  13. &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;
  14. &lt;/div&gt;
  15. &lt;div class=&quot;col-2&quot;&gt;
  16. &lt;h5&gt; password&lt;h5&gt;
  17. &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;
  18. &lt;/div&gt;
  19. &lt;div class=&quot;col-6&quot;&gt;
  20. &lt;h5&gt; User type&lt;h5&gt;
  21. &lt;select class=&quot;form-select&quot; aria-label=&quot;Default select example&quot; name=&quot;type&quot;&gt;
  22. &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;
  23. &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;
  24. &lt;/select&gt;
  25. &lt;/div&gt;
  26. &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 中接收按钮点击的响应。

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

  1. <form action="" method="post">
  2. <div class="row inputRow">
  3. <div class="col-2">
  4. <h5>名字</h5>
  5. <input type="text" class="form-control" placeholder="名字" name="名字" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $名字;} ?>">
  6. </div>
  7. <div class="col-2">
  8. <h5>姓氏</h5>
  9. <input type="text" class="form-control" placeholder="姓氏" name="姓氏" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $姓氏;} ?>">
  10. </div>
  11. <div class="col-2">
  12. <h5>用户名</h5>
  13. <input type="text" class="form-control" placeholder="用户名" name="用户名" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $用户名;} ?>">
  14. </div>
  15. <div class="col-2">
  16. <h5>密码</h5>
  17. <input type="text" class="form-control" placeholder="密码" name="密码" value="<?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){ echo $密码;} ?>">
  18. </div>
  19. <div class="col-6">
  20. <h5>用户类型</h5>
  21. <select class="form-select" aria-label="默认选择示例" name="类型">
  22. <option value="管理员" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='管理员'){ echo 'selected';} ?>>管理员</option>
  23. <option value="客户" <?php if (isset($_GET['trn']) && $_GET['trn']=='UPDATE' && $id=='客户'){ echo 'selected';} ?>>客户</option>
  24. </select>
  25. </div>
  26. <?php
  27. if (isset($_GET['trn']) && $_GET['trn']=='UPDATE'){
  28. echo '<button type="submit" class="btn btn-primary addOrder" name="UpdateBtn">更新列表</button>';
  29. }else{
  30. echo '<button type="submit" class="btn btn-primary addOrder" name="AddOrder">添加列表</button>';
  31. }
  32. ?>
  33. </div>
  34. </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.

  1. &lt;form action=&quot;&quot; method=&quot;post&quot; &gt;
  2. &lt;div class=&quot;row inputRow&quot;&gt;
  3. &lt;div class=&quot;col-2&quot;&gt;
  4. &lt;h5&gt; first name&lt;h5&gt;
  5. &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;
  6. &lt;/div&gt;
  7. &lt;div class=&quot;col-2&quot;&gt;
  8. &lt;h5&gt; last name&lt;h5&gt;
  9. &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;
  10. &lt;/div&gt;
  11. &lt;div class=&quot;col-2&quot;&gt;
  12. &lt;h5&gt; username&lt;h5&gt;
  13. &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;
  14. &lt;/div&gt;
  15. &lt;div class=&quot;col-2&quot;&gt;
  16. &lt;h5&gt; password&lt;h5&gt;
  17. &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;
  18. &lt;/div&gt;
  19. &lt;div class=&quot;col-6&quot;&gt;
  20. &lt;h5&gt; User type&lt;h5&gt;
  21. &lt;select class=&quot;form-select&quot; aria-label=&quot;Default select example&quot; name=&quot;type&quot;&gt;
  22. &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;
  23. &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;
  24. &lt;/select&gt;
  25. &lt;/div&gt;
  26. &lt;?php
  27. if (isset($_GET[&#39;trn&#39;]) &amp;&amp; $_GET[&#39;trn&#39;]==&#39;UPDATE&#39;){
  28. 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;;
  29. }else{
  30. 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;;
  31. }
  32. ?&gt;
  33. &lt;/div&gt;
  34. &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:

确定