0

I have a validation check on my site like this

'username' => array('required' => true,'min' => 2,'max' => 50,'unique' => 'users'),

so the username needs to be required, minimum of 2 and maximum of 50 characters, and unique in the 'users' table.

I want to add the postal code. Problem is, it needs to be different depending the country. That country is in a select:

<select class="select" name="land" id="land" onchange="if (this.selectedIndex) land();">
  <option value="-1">--</option>
  <option value="c1">Country 1</option>
  <option value="c2">Country 2</option>
  <option value="c3">Country 3</option>
</select>

In the JS code, I have this:

function land(){
    
    $("select.land").change(function() {
        var selectedCountry = $(this).children("option:selected").val();
        if(selectedCountry === 'Belgie'){
            <?php $postalCode = "'postcode' => array('required' => true,'figures' => true,'max' => 4, 'notlowercase' => true, 'notuppercase' => true, 'notspecial' => true)," ?>
        }else if(selectedCountry === 'Nederland'){
            <?php $postalCode = "'postcode' => array('required' => true,'figures' => true,'max' => 7, 'notlowercase' => true, 'uppercase' => true, 'notspecial' => true)," ?>
        }
    });
}

problem is, if I put in just the $postalCode in the validation, like this:

'username' => array('required' => true,'min' => 2,'max' => 50,'unique' => 'users'),
$postalCode,
...

then php gives me an Invalid argument on the foreach, that starts like this:

foreach($rules as $rule => $rule_value) {

Question is, how would I fix this?

--------Update----------- Now I have this:

$land = $_POST['land']; 
if($land === 'Belgie' || $land === 'belgie'){ 
  $test = "'postcode' => array('required' => true,'figures' => true,'max' => 4,'notlowercase' => true,'notuppercase' => true, 'notspecial' => true),"; 
}else if($land === 'Nederland' || $land === 'nederland'){ 
  $test = "'postcode' => array('required' => true,'figures' => true,'max' => 6, 'notlowercase' => true, 'uppercase' => true, 'notspecial' => true),"; 
} 

But still the same error (invalid argument). I think it's because I give a String and it expects an Array, but don't know how to solve this.

user3906610
  • 99
  • 1
  • 10
  • You can't pass variables from JS to PHP without an http request. You need to pass the country from the select to your PHP and perform the validation in the PHP based on the passed value. You might find [this Q&A](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) helpful. – Nick Sep 06 '20 at 00:05
  • see update please – user3906610 Sep 06 '20 at 09:20
  • 1
    The original question you asked is a common mistake in understanding how JS and PHP relate to each other. Your edit is less clearly so, but doesn't include a [mcve] so can't really be answered. If you have a PHP-only version of the code that's not working, try to break it down to the shortest code you can reproduce the error with, and if you don't spot the problem yourself, ask a new question, remembering to include the exact code you've run and the exact output you got. – IMSoP Sep 06 '20 at 09:29

0 Answers0