0

Pls help..

I have a dynamic form in which the no. of elements are not fixed, and i need to perform JS validation on the form. I have a PHP function that will return me an array of all elements that are currently displayed in form. I want to use this in the JS code..

I am trying something lyk dis::

function checkValidation() {

  <?php echo  
    // php function call
    $arrColumnsInfo = $this->fetch_fields_info();
    print('<pre>');
    print_r($arrColumnsInfo);
    print('</pre>');
  ?>
}

Not working for me..throws JS error.. OR this..

function checkValidation() {
  <?php=
    // php function call 
    $arrColumnsInfo = $this->fetch_fields_info();?>
    alert(<?=$arrColumnsInfo?>);
}

Again.. not working..

My PHP array output:

Array
(
[client_code] => Client Code::1::Basic company Info::desc::TB
[entity_name] => Entity Name::1::Basic company Info::::TB
[type_of_entity] => Type of entity::1::Basic company Info::Type of entity,Sole trader,Partnership,Australian Private Company, other::DD
[fixed_fees] => fixed fees::8::Fees Related::yes,no::RD
[billing_time] => Billing time::8::Fees Related::Weekly,Fortnightly,Monthly,Yearly::DD
)

The key here used is the name of form element.. eg. i am building my form lyk dis..

<input type="text" name="client_code" value="">... 

and so on.. and in the same way all form elements are dynamically created. And now i want to validate this form so i again need this array in javascript.

6
  • defining array in js is slightly different then PHP.. Commented Aug 25, 2012 at 5:12
  • but there must be a possibility to access PHP array in JS code. Commented Aug 25, 2012 at 5:16
  • You need to validate each of your dynamically generated elements before the form submits ?? Commented Aug 25, 2012 at 5:23
  • Have a look at php.net/manual/en/function.json-encode.php unless you simply add required to each field that needs to be validated and have JavaScript look for that Commented Aug 25, 2012 at 5:24
  • How does the dynamic field look like ? Commented Aug 25, 2012 at 5:24

3 Answers 3

1

You could use json_encode to transform your PHP Array into a from that can be used by JavaScript.

function checkValidation() {
  var data = <?php echo json_encode($this->fetch_fields_info()) ?>;
  // ... run your client-side checks
  alert(data);
}

Just keep in mind, that you always have to provide valid JavaScript code. That can sometimes be quite tricky if you insist on writing that JavaScript code with inline PHP.

You should at least consider doing your validation on the server-side (in PHP) and only passing the result to your client.

2
  • 1
    hey.. i tried wd ur code. it does not works for me. when i alert(data) it gives me empty result. Commented Aug 25, 2012 at 5:39
  • try var data = '<?php echo json_encode($this->fetch_fields_info()) ?>'; Commented Aug 25, 2012 at 16:07
0

your php code have to generate the result something like this:

var arr = <?php echo $arr; // result: [1,2,3,4,5,6,7] ?>

or the other array() format that support in JavaScript..

it is just an example.

take a look on the give link : Array in Javascript this link tells you that what format you need to use an Array.

-2

data is not sufficient to help you @disha. please provide some more and edit this . also what is this '=' in you first line of php? provide some more details that someone can really help you. Happy coding!

2
  • This must be a comment ..not even near to an answer Commented Aug 25, 2012 at 5:21
  • i am just trying it in a way.. i need to call this PHP function. so trying something like dis.. Commented Aug 25, 2012 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.