
Originally Posted by
Gathoon
I don't even know if it is possible to use both javascript and PHP in one site.
Of course it is possible. Javascript is client-side and php is server-side.
The problem is - you CAN'T create popup with php. You can do all logic in php, but alerts must be created with javascript.
for php solutiuon:
this is your form:
Code:
<form action="index.php" method="POST" enctype="multipart/form-data">
<h2>Title</h2>
<input class="form" type="text" name="title"/>
<br/><br/>
<h2>Content</h2>
<textarea class="form" name ="textentry" rows="3"></textarea>
<input type="hidden" name="submitted" value="1">
<br/><br/>
<input type="submit" value="Submit"/>
</form>
Note input field with type="hidden". It's important. with it we will check if form was already submitted, or not.
like this:
Code:
if ($_POST['submitted']==1) {
//something
}
Also action="index.php" - index.php is actually the page where the form is.
and next we will add some validation:
Code:
<?php
if ($_POST['submitted']==1) {
$errorMsg = ""; // error messages variable
if (!empty($_POST['title'])){ // check if title was entered
$title = $_POST['title'];
}
else{
$errorMsg = "You must enter title"; // and if not - add error message
}
if ($_POST['textentry']){ // check if content was entered
$textentry = $_POST['textentry'];
}
else{
if (!empty($errorMsg)){ // if there is already an error, add next error
$errorMsg = $errorMsg . " & content";
}else{
$errorMsg = "You must enter content";
}
}
if (!empty($errorMsg)){ // if there is errors display them
echo ' <script type="text/javascript">
alert("'.$errorMsg.'");
</script>
';
}
}
?>
As you can see we display errors with javascript popup at the end.
Bookmarks