2021年3月14日星期日

PHP Cannot get post data from form

I want to submit a form that user fill up their information to an api. I have created a wordpress plugin to do it. The problem that I am currently facing is when I call $_POST["registerId"] or $_POST["password"] it is empty value.

The following code is all belong to the same page. This function consist of the form and it will call javascript to do validation.

function registration_form_layout() {        ?>        <h2>Registration Form</h2>        <script type="text/javascript">                     function validate() {                // Prevent trigger and initialize error              event.preventDefault()              var error=""              var isError = false                // Retrieve value of the uid, password and confirm password field              var uid = document.getElementById("form-uid").value;              var password = document.getElementById("form-password").value;              var confirmPassword = document.getElementById("form-confirm-password").value;                // Field declaration              var uidField = "form-uid"              var uidErrorField = "error-message-uid"                var passwordField = "form-password"              var passwordErrorField = "error-message-password"                var confirmPasswordField = "form-confirm-password"              var confirmPasswordErrorField = "error-message-confirm-password"                var firstUIDCharacter = uid.charAt(0)                  initializeField();                if ( uid === "" || uid.length < 6 || uid.length > 15 || (firstUIDCharacter >= '0' && firstUIDCharacter <= '9') ) {                  error = "Please enter correct UID"                  generateErrorField(uidField, uidErrorField, error)                  isError = true              }                 if ( uid.match(/\d{3,}/) ){                  error="UID last three characters cannot be all numbers"                  generateErrorField(uidField, uidErrorField, error)                  isError = true              }                if ( password === "" || password.length < 6 ) {                  error = "Please enter correct password"                  generateErrorField(passwordField, passwordErrorField, error)                  isError = true              }                 if ( confirmPassword === "" ) {                  error = "Please enter correct password"                  generateErrorField(confirmPasswordField, confirmPasswordErrorField, error)                  isError = true              }                if ( password !== confirmPassword ) {                  error = "The two inputs must be consistent"                  generateErrorField(passwordField, passwordErrorField, error)                  generateErrorField(confirmPasswordField, confirmPasswordErrorField, error)                  isError = true              }                if ( password.length > 5 && ( password.match(/^-?\d+$/) || password.search(/[a-zA-Z]/) === -1 ) ) {                  error = "Password must contain both letters and numbers"                  generateErrorField(passwordField, passwordErrorField, error)                  generateErrorField(confirmPasswordField, confirmPasswordErrorField, error)                  isError = true              }                if (isError) {                  return false              } else {                  var recommenderID = "TPAxvP/Ku88c/LZ0rbdZGoNabSZ903llxaX79uFa45lAtmBQatgLVRgLLj4yy094ZMR0DN3gfhsWrXnuleLBrinfNI4djptLk+zfBpaNqez27j+l6AINA8Neixc";                        var result = "<?php echo submit_registration(); ?>"                  console.log(result)                                }          }        </script>      <p id="php_code"></p>        <form method="POST" id="registration-form" onsubmit="return validate()">          <label>UID</label>          <br/>          <input type="text" name="registerId" id="form-uid" class="form-control" placeholder="Please enter your login ID"/>          <p id="error-message-uid" class="error"></p>            <label>Password</label>          <br/>          <input type="password" name="password" id="form-password" class="form-control" placeholder="Please set the new password"/>          <p id="error-message-password" class="error"></p>            <label>Confirm Password</label>          <br/>          <input type="password" name="confirm_password" id="form-confirm-password" class="form-control" placeholder="Please confirm the new password"/>          <p id="error-message-confirm-password" class="error"></p>          <br/>            <br/>          <input type="submit" name="form_submit" id="submit-registration" class="form-control" value="Next"/>          <br/>      </form>        <?php  }  add_shortcode('register_form','registration_form_layout');  

This part of the code will trigger the api and use the $_POST data to submit the data to the api.

function submit_registration() {        $ch = curl_init();        curl_setopt($ch, CURLOPT_URL,"https://localhost/testing/register");      curl_setopt($ch, CURLOPT_POST, 1);        $payload = json_encode(           array(               "Password"=> $_POST["password"],               "RegId"=> $_POST["registerId"],           )       );        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload );      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        $server_output = curl_exec($ch);      $error    = curl_error($ch);      $errno    = curl_errno($ch);      $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);        $test = json_decode($server_output, JSON_PRETTY_PRINT);        echo '<pre>UID: '.$_POST["registerId"].'Password: '.$_POST["password"].'Server Output:'.status_message($test['Status']);        curl_close ($ch);   }  
https://stackoverflow.com/questions/66631512/php-cannot-get-post-data-from-form March 15, 2021 at 10:07AM

没有评论:

发表评论