Bạn muốn hiển thị thông tin tự động nhập vào các ô trong trang đăng ký khi họ truy cập vào đường dẫn chứa chuỗi truy vấn (Parameter) bằng php, có thể áp dụng trong wordpress. Dưới đây là các bạn có thể sử dụng:
Ví dụ bạn có 1 trang đăng ký là: https://gocchiase.net/dang-ky/ (Thay tên miền gocchiase.net bằng tên miền của bạn)
Bạn có form đăng ký như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<form class="form-horizontal" method="post" role="form"> <div class="form-group"> <label class="control-label col-sm-3" for="username">User name:</label> <div class="col-sm-9"> <input type="text" class="form-control" name="username" id="username" placeholder="User name" value="<?php if(isset($tripname)){ echo $tripname;}?>" readonly> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="email">Email:</label> <div class="col-sm-9"> <input type="email" class="form-control" name="email" id="email" placeholder="Email"> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="pwd1">Password:</label> <div class="col-sm-9"> <input type="password" class="form-control" name="pwd1" id="pwd1" placeholder="Enter password"> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="pwd2">Re-enter password:</label> <div class="col-sm-9"> <input type="password" class="form-control" name="pwd2" id="pwd2" placeholder="Re-enter password"> </div> </div> <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?> <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <button type="submit" class="btn btn-primary">Register</button> <input type="hidden" name="task" value="register" /> </div> </div> </form> |
Giờ bạn muốn khi truy chuỗi truy vấn sau: https://gocchiase.net/dang-ky/?user=gcc&email=gocchiase@gmail.com thì các trường User name và Email sẽ tự động hiển thị thông tin trong chuỗi truy vẫn.
Để tự động hiển thị User là gcc và Email là gocchiase@gmail.com vào các trường User và Email trong form đăng ký. Bạn làm như sau:
Trong mã php bạn khai báo các biến:
1 2 3 4 |
<?php $user=$_GET['user']; $email=$_GET['email']; ?> |
Tham khảo: https://qastack.vn/wordpress/111376/use-query-string-in-url-to-display-content-on-the-page
Tiếp đến bạn lấy dữ liệu vào ô Value trong form:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php $user=$_GET['user']; $email=$_GET['email']; ?> <form class="form-horizontal" method="post" role="form"> <div class="form-group"> <label class="control-label col-sm-3" for="username">User name:</label> <div class="col-sm-9"> <input type="text" class="form-control" name="username" id="username" placeholder="User name" <span style="color: #ff0000;" value="<?php if(isset($user)){ echo $user;}?>"> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="email">Email:</label> <div class="col-sm-9"> <input type="email" class="form-control" name="email" id="email" placeholder="Email" value="<?php if(isset($email)){ echo $email;}?>"> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="pwd1">Password:</label> <div class="col-sm-9"> <input type="password" class="form-control" name="pwd1" id="pwd1" placeholder="Enter password"> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="pwd2">Re-enter password:</label> <div class="col-sm-9"> <input type="password" class="form-control" name="pwd2" id="pwd2" placeholder="Re-enter password"> </div> </div> <?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?> <div class="form-group"> <div class="col-sm-offset-3 col-sm-9"> <button type="submit" class="btn btn-primary">Register</button> <input type="hidden" name="task" value="register" /> </div> </div> </form> |
Ok, bây giờ bạn truy cập vào link https://gocchiase.net/dang-ky/?user=gcc&email=gocchiase@gmail.com sẽ thấy form tự động nhập thông tin như sau:
Để sét các ô user name và Email chỉ hiển thị không cho người dùng chỉnh sửa bạn thêm thuộc tính readonly
như sau:
1 2 3 4 5 6 7 |
<input type="text" class="form-control" name="username" id="username" placeholder="User name" value="<?php if(isset($user)){ echo $user;}?>" readonly> </div> </div> <div class="form-group"> <label class="control-label col-sm-3" for="email">Email:</label> <div class="col-sm-9"> <input type="email" class="form-control" name="email" id="email" placeholder="Email" value="<?php if(isset($email)){ echo $email;}?>" readonly> |
Tham khảo: https://www.w3schools.com/tags/att_input_readonly.asp
Như vậy là bạn đã biết các sử dụng chuỗi truy vấn (Parameter) để tự động hiển thị thông tin trên trang web php. Chúc bạn thành công!