简体   繁体   中英

How to compare $_POST values with array values in Ajax and PHP?

Let's say I have one array with 10 elements and 10 inputs in a form. How would I compare on links click first value of the array with the value in the first input with Ajax? And so for the next ones.

I have to mention that I can put yes or no in the empty values of every input.

$array = array(
      1 => 'yes',
      2 => 'no',
      3 => 'yes'
);

 <form action='' method='post'> <input type='text' name='input1' value=''> <input type='text' name='input2' value=''> <input type='text' name='input3' value=''> <a href='' class='yes'>Yes</a> <a href='' class='no'>No</a> </form> 

You can try as following

<script>
function comp()
{
    var jqueryarray = <?php echo json_encode($myvalues); ?>;
    var inputVal1 = $('#input1').val();
    var inputVal3 = $('#input2').val();
    var inputVal3 = $('#input3').val();

    // you can now compare inputVal1 or inputVal2 or inputVal3 with array jqueryarray which we have created from php array
}
</script>

<?php $myvalues = array(1 => 'yes', 2 => 'no', 3 => 'yes'); ?>

<form action='' method='post'>
  <input type='text' name='input1' id='input1' value=''>
  <input type='text' name='input2' id='input2' value=''>
  <input type='text' name='input3' id='input3' value=''>
  <a href='' class='yes' onclick='comp()'>Yes</a>
  <a href='' class='no' onclick='comp()'>No</a>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM