Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Programming (http://www.go4expert.com/forums/programming-forum/)
-   -   Using PHP variables in a Javascript function (http://www.go4expert.com/forums/using-php-variables-javascript-function-t20388/)

tommyp 17Dec2009 02:18

Using PHP variables in a Javascript function
 
I am trying to add a PHP variable $i, into a form element using a Javascript function, however this doesnt seem to be working at all, does anyone have any ideas why this may be? I am a novice coder so it may be something obvious. Any help please?

My code is:
HTML Code:

        <script type="text/javascript">
        function addvote() {
        var f = document.getElementById('vote');
        var entry_num = document.getElementById('entry_num')
        alert("document.write(entry_num)";
        document.forms.vote.entry_num.value=("<?php echo $i; ?>");
            if (f.entry_num.value!="")    {
            alert ("submitting vote");
                f.submit();
                                        }
                    else{
                        alert("error")
                        }
                          }
        </script>

Thanks

venami 17Dec2009 07:51

Re: Using PHP variables in a Javascript function
 
Instead do like this:

PHP Code:

<?php
print "<script type=\"text/javascript\">\n";
print 
"function addvote() {\n";
print 
"var f = document.getElementById('vote');\n";
print 
"var entry_num = document.getElementById('entry_num')\n";
print 
"alert(\"document.write(entry_num)\";\n";
print 
"document.forms.vote.entry_num.value=(" $i ");\n";
print 
"if (f.entry_num.value!=\"\") {\n";
print 
"alert (\"submitting vote\");\n";
print 
"f.submit();\n";
print 
"}\n";
print 
"else{\n";
print 
"alert(\"error\")\n";
print 
"}\n";
print 
"}\n";
print 
"</script>\n";
?>

This must work now.


All times are GMT +5.5. The time now is 06:31.