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">

Recommended Answers

That is a Javascript function. You cannot just include it into a PHP script like that.

Jump to Post

All 3 Replies

That is a Javascript function. You cannot just include it into a PHP script like that.

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">

I should've make a file for js.

That was an easy code! Haha
Thank you for educating and helping.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.