Home > OS >  How do I implement a presence check in php for all name
How do I implement a presence check in php for all name

Time:04-16

<?php
for($x=1;$x<=$_GET['numberOffamembers'];$x  ){
?>
  <table>
    <tr>
      <td>Family Member:<?=$x?></td>
      <td><textarea tabindex="2" id="message"  name="Name:<?=$x?>" rows="1" cols="50"></textarea></td>
    </tr>
    <tr>
      <td>Phone No:</td>
      <td><textarea tabindex="2" id="message"  name="PhoneNo:<?=$x?>" rows="1" cols="50"></textarea></td>
    </tr>
    <tr>
      <td colspan="2"><hr width="1150px"></td></tr>
  </table>
<?php
}

Where $_GET['numberOffamembers'] is a variable that the user has entered before. I basically want it so that a user could enter the number of family members and then enter the specific names and phone numbers if they have one. I am just not sure how "family member" is stored so am unsure how I can do validation.

CodePudding user response:

You can use name array in html input field i.e fam_member[] and wrap the code in a form with a submit button.

<form action="some_url" method = "POST">
 <?php
    for($x=1;$x<=$_GET['numberOffamembers'];$x  ){
    ?>
      <table>
        <tr>
          <td>Family Member:<?=$x?></td>
          <td><textarea tabindex="2" id="message"  name="fam_member[<?=$x?>]" rows="1" cols="50"></textarea></td>
        </tr>
        <tr>
          <td>Phone No:</td>
          <td><textarea tabindex="2" id="message"  name="phone_no[<?=$x?>]" rows="1" cols="50"></textarea></td>
        </tr>
        <tr>
          <td colspan="2"><hr width="1150px"></td></tr>
      </table>
    <?php
    }
?>
<input type="submit" name="Submit">
</form>

At the time of submission you can iterate the array to get it stored in database,

<?php 
if(isset($_POST["Submit"])){
    foreach($_POST['fam_member'] as $key=>$value){
        // your code to store it in database.
    }
}
?>

CodePudding user response:

you can have some approaches for achieving what you want, but the best and most reasonable one is to just use javascript or one of js based libraries for validation, as an example you can use parsley.js then simply create a custom validator for checking the uniqness of the Family Member field by such an ajax code in that custom validator :

// unique Family Member validator 
 window.Parsley.addValidator('unique_family_member', {
    validateString: function (value, requirement) {
        xhr = $.ajax({
            url: "check.php",
            dataType: 'json',
            method: 'GET',
            data: {
                action: 'checkFamilyMemberExists',
                familyMember: value
            }
        });
        return xhr.then(function (data) {
            console.log((data.isUnique == 0));

            if (data.isUnique == 0) {
                return true;
            } else {
                return $.Deferred().reject();
            }
        });
    },
    messages: {
        en: 'Username already exists!',
    },
        priority: 32
    });
  

in check.php file, you must retrieve its value from the database.

  • Related