0

I'm trying to clear a value in a textbox whenever i click the clear button. But i keep on getting a syntax error expected "=" in the line document.getElementByid. Is my code wrong?

 <?php

function clear(){
document.getElementByid('textinput').value="";
}

if (isset($_POST['textinput'])){
  echo $_POST['textinput'];
}

if (isset($_POST['clear'])){
clear();
}
?>


<form action="index.php" method="post">
<input type="text" name="textinput">
<input type="submit" value="Post" name="post">
</form>

<input type="submit" value="Clear" name="clear">
3
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by Eggstraordinary
0

pritaeas is right, you cannot mix call Javascript functions from PHP like you are trying to do. There are ways to achieve a similar effect, but there are better ways altogether. Try something like this. I would even take it further and merge the to if statements into an if-else, checking for $_POST['clear'] first and then echoing $_POST['textinput'] if clear was not found. However, I didn't do it that way because I wanted to make as few changes to your code as possible.

 <?php

if (isset($_POST['textinput'])){
  echo $_POST['textinput'];
}

if (isset($_POST['clear'])){
    $_POST['textinput'] = '';
}
?>


<form action="index.php" method="post">
<input type="text" name="textinput" value="<?php echo $_POST['textinput']; ?>">
<input type="submit" value="Post" name="post">
</form>

<input type="submit" value="Clear" name="clear">
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.