0

I'm a beginner to PHP and I have a quick question. Is it possible to have javscript code injected into a document through php?

For example, suppose I wanted to inject a button that called a function after a form is filled out:

<html>
<head>
  <script type = text/javascript>
  function func() {
  ...
  }
  </script>
</head>
<body>

<form action = 'welcome.php' method = 'post>
Name: <input type = 'text' name = 'fname' />
<input type = 'submit' value = 'submit' />
</form>

<?php
if (isset($_POST['fname'])) {
   echo '<input type = button onclick = func />'
}
?>

</body>
</html>

Would this work? If not, why not?

Any help would be great

2
  • It will work, providing you close your string inside the if(); PHP is a templating language after all. Commented Dec 5, 2011 at 17:08
  • 1
    Yes you can echo out javascript through php. Commented Dec 5, 2011 at 17:08

3 Answers 3

1

Will work fine - just remember to close your quoted strings :

<form action='welcome.php' method='post'> // add ' here
Name: <input type='text' name='fname' />
<input type='submit' value='submit' />  // removed a couple of spaces (not required)
</form>

<?php
if (isset($_POST['fname'])) {
   echo '<input type="button" onclick="func" />';   // added some quotes and a semicolon
}
?>

I have removed the spaces between the attributes and the values in HTML from type = "something" to type="something" although I can't find the "official" specification on this. It seems that the spaces are perfectly valid ...I think its my personal preference to have no white space there .....

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to add a trailing semicolon to the echo statement!
0

Try:

echo '<input type="button" onclick="' . $_POST['fname'] . '" />';

SAMPLE

If fname=doSomething then use:

echo '<input type="button" onclick="' . $_POST['fname'] . '()" />';

and your rendered HTML would be:

<input type="button" onclick="doSomething()" />

Then you'd just need your function somewhere:

<script>
  function doSomething() { }
</script>

5 Comments

@Matt_H-- how is the function called if onclick calls the POST variable?
Added an example of what it would look like, also adding the ()
@Matt_H--I think theres some slight confusion. 'fname' is the input into the text box, not the name of the function.
I know - I'm assuming the fname POST variable holds the name of the function you are trying to call.
FYI, if you do this, then a person who submits your form can choose any function they want to run simply by typing it into a form field that's POST'd to your php script. There's too much confusion here.
0

If your current page is welcome.php and you ran this code, you would press the Submit button and the page would reload and have your new button.

But the best way to see whether this works for you or not is to run it. Why don't you try that first?

Comments

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.