0

As I am new to this I would like your advice.
So far I have figured out two ways to populate form fields.
Say this is my html:

 <input id="test" name="test" type="text">  

As you see the id of each field matches the name.
When it's time to populate the field from Sql i could either do:

PHP:

 <input id="test" name="test" type="text" value="<?php if(isset($SqlRequested['test'){echo $SqlRequested['test'];}">  

and similarily write all the fields of my form...
or add this in the head part
JQUERY:

<script>
 <?php  echo "$(document).ready(function(){";
        foreach ($SqlRequested as $id => $value){
                echo "$('#" . $id . "').val(\"" . $value . "\");"
                echo "};"
         echo "});"
 ?>
 </script>

besides the fact that the jQuery gives me some problems when the fields contain "enter/return" in them (which could be fixed by using .html or .text i guess)

Which one would you suggest, what are the drawbacks or pro's of each method?
Any improovements to the code?
Is there some other way i am missing?
Any suggestions on easily fixing the little problem mentioned above?

Thanx in advance.

Edit: I just noticed the jQuery .populate, which im guessing is something like my second way, you just need to get your data in a format of {'id':'value',...}

Iridio
  • 9,213
  • 4
  • 49
  • 71
krasatos
  • 1,177
  • 3
  • 13
  • 26

2 Answers2

3

You should use PHP to render the page as someone without javascript wouldn't get the values of your javascript version.

PHP: Pros: All browsers will get what you want them to get. The output HTML is arguably more correct as it has the value that the user expects in it

Cons: ...Increased server load....by such a small amount that it should probably not even be a con

JS: Pros: ...

Cons: Non JS enabled browsers won't see the values you expect.

Improvements: Write a function in PHP that will auto echo your value if it exists to cut down on duplicate typing. Something like:

function echoValue($value) {
global $SqlRequested;
    if(isset($value)){
        echo $value;
    }
}

Or if you wanted:

function echoValue($value) {
global $SqlRequested;
    if(isset($SqlRequested[$value])){
        echo $SqlRequested[$value];
    }
}   
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Blair McMillan
  • 5,299
  • 2
  • 25
  • 45
  • so adding this function to the top of my page my html should be `` correct? – krasatos Apr 05 '12 at 07:05
  • Not quite, I've updated the code because it wasn't right. Using the example code there now it would be ``. If you wanted to use `` you would use the second version of the function. – Blair McMillan Apr 05 '12 at 07:09
  • Thank you, that makes sense to me. Is there some way for php to read the id/name of the input that the code is in? something like (in my own imaginative language): `` or am i asking too much? :) – krasatos Apr 05 '12 at 07:13
  • 2
    Genuine quesiton: Who wouldn't have javascript? An example? – Flukey Apr 05 '12 at 07:19
  • Asking too much for how it is currently :) The only way for that to possibly happen would be to have an array in PHP of all of the fields that you want to output and then use PHP to render the actual HTML as well. If you want to go down this line, have a look for "PHP form classes". I'm guessing this will be a bit of an overkill for what you are doing currently though. – Blair McMillan Apr 05 '12 at 07:21
  • @Flukey A surprising number of office environments have javascript disabled and the computers locked down so much that it can't be enabled. See this great question and answer for more info http://stackoverflow.com/questions/9478737/browser-statistics-on-javascript-disabled – Blair McMillan Apr 05 '12 at 07:25
  • thank you, i should add the fact that my form is quite long... around 300 inputs/textareas etc. I should still go with the PHP way right? – krasatos Apr 05 '12 at 07:28
  • That Yahoo! data is 2 years old.That percentage has likely reduced (due to the advances of web technology and smartphones). So, in the UK for example it's probably lower than the 1% mark. It's seems ridiculous that so many developers spend so much time making sure their rich web app degrades nicely for users without javascript just to support less than 1% of users. Exactly like developers who still hack their code to support IE6. It boggles the mind. – Flukey Apr 05 '12 at 07:31
  • for some reason i cannot figure out... the function doesnt work, it gives me undefined variable $whatever although the code when inline html produces the desirable result... any ideas? – krasatos Apr 05 '12 at 08:44
  • i figured it out, some parenthesis were missing, plus you need to call the global $sqlrequested inside the function. Made the edits, they need to be approved. Anyway, it works for me now. cheers again – krasatos Apr 05 '12 at 09:31
  • @Flukey It's always something that has to be decided either by yourself, or the business you are representing. Is it worth spending the time (and therefore money) to support something that will only affect a (relatively) small number of people. You're clearly of the opinion that the work you do isn't worth supporting old stuff. My answer was just showing that there is an issue that should be *considered*. Also, I don't like saying "oh, it's probably less than that now...", however correct it may be. We need a new survey that's up to date. As for IE6... some businesses need to support it. :( – Blair McMillan Apr 10 '12 at 00:08
0

Nice question - all of this methods using in different frameworks, cms etc. Most "simple" and direct method is

value="<?php echoValue('test');?>"

Most powerful and "complex" is use framework like knockout.js for render forms.

And yes - "Who wouldn`t have javascript?"

Axiom - All have javascript.

user1303559
  • 436
  • 3
  • 10