Lets say i have a form that asks for name and exam result. Id like to make the form disappear and display another div when the user hits submit.
heres the form code
<form action="exam.php" method="post" id = "coursedata" class="form-horizontal">
<fieldset>
<legend>Please Complete the Details</legend>
<div class ="form-group">
<label for="name" class="control-label col-xs-2">Student name</label>
<div class="col-xs-6">
<input type="text" name="name" id="name" size="20" class="form-control" autofocus required pattern = "[A-Z][a-z]+( [A-Z][a-z]+)?" title="Please enter a name (Surname optional) with First Letter capitalised">
</div></div>
<div class ="form-group">
<label for="CA1" class="control-label col-xs-2">Assessment One</label>
<div class="col-xs-6">
<input type="number" class="form-control" min = "0" max = "100"name="CA1" id="CA1" size="3" maxlength ="3" onkey ="limit(this);" onkeyup="limit(this);" required>
</div></div>
<div class="buttonarea">
<input type="submit" value="Submit" name = "Submit">
<input type="reset" value="Clear the Info">
</div>
</fieldset>
</form>
The shortest way (asuming that form and exam.php are the same files...):
<?php
if (!isset($_POST['Submit'])) {
?>
Your form code
<?php
}
else {
// form engine
echo "<div>SUCCESS</div>";
}
?>
Hope it helps :). PS. This is a strict answer to Your question, whether there are many other questions, like how to handle form, how to verify it, etc. But this is another side of answered coin ;). Best regards :).
Use jQuery for this (JavaScript library).
Copy this in your head section
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button[type="submit"]').click(function(){
$("#coursedata").hide();
return false;
})
})
</script>
Related
I need to code an AJAX request with jQuery (3.2.1) and display email input value after the form was sended.
When I click on submit button, i have this following message, I didn't understand what is wrong with my code.
<form action="process.php" method="post" id="myForm">
<div class="form-line">
<input type="email" name="emailaddress" id="emailaddress" />
</div>
<div id="emailMsg"></div>
<div class="form-line">
<input type="password" name="psw" id="password" />
<button type="submit" id="submit"><span>OK</span></button>
</div>
<div id="passMsg"></div>
</form>
And this is what i have done on jQuery code
$("#myForm").on('submit',function(e) {
e.preventDefault();
var email = $("#emailaddress").val();
$.post("../process.php", {
emailaddress:emailaddress
}, function(response) {
$("#connexion-rollover").html(email);
});
});
I have also create this PHP file to add this code
<?php
echo $_POST['emailaddress'];
?>
I hope that my first message on this site was clear.
I am using a jQuery function which work to hide placeholder text on form focus, this functions works perfectly on the form in main page (Index.php), however it is not working on other forms which are designed using bootstrap (Members.php) as an example. I have tried to see where is the issue but it seems not working. I don't know if version incompatibility of both Bootstrap and jQuery caused this issue?!
I am using
- jquery-3.4.1.min.js
- bootstrap-3.3.7
=========================Backend.js=================================
// here is the jQuery function
$(function(){
'use strict';
// Hide Placeholder On Form Focus
$('[placeholder]').focus(function (){
$(this).attr('data-text',$(this).attr('placeholder'));
$(this).attr('placeholder', '');
}).blur(function (){
$(this).attr('placeholder',$(this).attr('data-text'));
});
});
==============================Index.php=====================================
// Here is the form where the jQuery function works fine
<form class="login" action="<?php echo $_SERVER['PHP_SELF']?>" method = "POST">
<h4 class="text-center">Admin Login</h4>
<input class = "form-control input-lg" type="text" name="user" placeholder="Username" autocomplete="off"/>
<input class = "form-control input-lg" type="password" name="pass" placeholder="Password" autocomplete="new-password"/>
<input class = "btn btn-lg btn-primary btn-block" type="submit" value="Login" />
</form>
===============================Members.php===================================
// Function doesn't work on this form
<div class="container">
<form class="form-horizontal" action="?do=Insert" method="POST">
<!-- start username field -->
<div class="form-group">
<label class="col-sm-2 control-label" >Username</label>
<div class="col-sm-8">
<input type="text" name="username" class="form-control" autocomplete="off" required = "required" placeholder="Enter User Name" />
</div>
</div>
</form>
</div>
var input = $("input");
input.focus(function(){
$(this).attr("placeholder", " ")
});
input.blur(function(){
$(this).attr("placeholder", "example")
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" placeholder="example">
I am wondering if there is a way to show a title box containing a message when you click a button,or as a better example,you have an input,and after php script evalute the value,and it's wrong,a title box will show up , saying "Wrong" ... Any ideas? Just with php if its possible!!
EDIT:
What i am looking for,is not for a simple message like:
echo '<p></p>';
Of course i know how to do that,its basic...
I was just wondering if a TITLE box can show up,like this:
<input type=text title='Wrong'>
but after the value its evaluated...
You could achieve this by setting a GET or POST value with the button:
html:
<form method="post">
<input type="hidden" name="postvalue" value="yes">
</form>
php:
<?php
if(isset($_POST['postvalue'])){
echo "<p>messageboxtoshow</p>";
}
?>
however a solution with javascript would work way better and would look way more userfriendly, and this only works on page refresh.
Try this sample one
<?php
if(isset($_POST['submit'])){
echo "<p> Your message</p>";
}
?>
<form action=<?php $_SERVER['PHP_SELF']?> method="post" >
<input type="submit" name="submit" value="submit">
</form>
Do you mean validation per input field?
You can use JQuery for that.
I made a fiddle to give you a visual idea: http://jsfiddle.net/4b3tzreo/
For example, if this is your form:
<div class="container">
<div class="left">
Username
</div>
<div class="right">
<input type="text" name="username" id="username" />
</div>
</div>
<div class="container">
<div class="left">
Password
</div>
<div class="right">
<input type="password" name="password" id="password" />
</div>
</div>
<div class="container">
<div class="left">
<button id="submit">Submit values</button>
</div>
</div>
You can add a class to it when the input value is empty, like this:
$('#submit').click(function(){
var username = $('#username').val();
var password = $('#password').val()
if(username === '') {
$('#username').addClass('error');
alert('username is not filled');
}
if(password === '') {
$('#password').addClass('error');
alert('password is not filled');
});
I am building a simple form script that collects a users email and returns a PIN.
The input sits in standard HTML below:
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
I have the following if statement that checks the database to see if the email already exists, and if the user would like a PIN reminder.
if($num_rows != 0) {//if email found in table
?>
Email already registered, would you like a PIN reminder?
<form method="POST" action="">
<input type="submit" name="srSend" value="Click here to get pin reminder" />
<input type="hidden" name="srEmail" value="<?php echo strtolower($email);?>" />
</form>
<?php
exit;
}
At the moment, this returns the result to the user as a new page; how do I put this in the actual HTML of the body page, so it would actually appear below the original form input in a new <p> element?
This is a piece of cake with jquery.post
include the jquery library in your html head and you'll need a short script to get the php content by ajax
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
$('#yourForm').submit(function(e){
e.preventDefault();
var data=$('#yourForm').serialize();
$.post('yourphp.php',data,function(html){
$(body).append(html);
});
});
});
</script>
</head>
<body>
<p>
<form id="yourForm" class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
</body>
You could able to achieve that without ajax too. Simply use a hidden iframe in your main page, then set the target attribute of your form to the iframe as follows:
<form method="post" action="page.php" target="myIframe">
.....
</form>
<p id="theResultP"></p>
<iframe src="#" name="myIframe" style="visibility: hidden"></iframe>
The question now, How could you make the page loaded in the iframe "page.php" to interact with the opener page to update the p. This may be done as follow:
//page.php
<script>
result = parent.document.getElementById('theResultP');
result.innerHtml = "<b>The message you want</b>"
</script>
I dont know if I get what you asking for,but this is my solution :
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
<?php
if (isset($message)){
<p><?php print $message ?></p>
?>
<?php
}
?>
and in top of your file write something like this :
<?php
if($_post['srSend']){
$message='write your message here';
}
?>
Don´t know if my english is sufficient to explain this right:
I have made a Form that is needs 4 or 5 pages until final submit comes.
On every change the Post is added to the new page as a hidden field and a new script is called.
<form method="post" action="form4.php">
My problem is i dont want the browser to show the url to the new script and ideally keep it at the origin url.
I dont know the right terms to search on google or here for a solution. Maybe you can give me a hint how to accomplish this?
Another option is to handle the form by the same script in stages.
<form action="formhandler.php" method="post">
<input type="hidden" name="form_stage" value="1" />
....
</form>
and within the formhandler.php:
switch($_POST['form_stage']) {
case 5:
// handle the stage 5 stuff here
break;
case 4:
// handle stage 4 stuff here, output form for stage 5
break;
case 3:
// handle stage 3 stuff here, output form for stage 4
break;
case 2:
// ditto
break;
case 1:
default:
// display initial form here
}
Of course, you don't have to use a switch for this. A long sequence of if/else if works just as well, it comes down to personal preference/code complexity. But this basic workflow allows you to keep the same url and still display multiple different forms without having to resort to AJAX/javascript.
If the action of the form is form4.php, then you can't change which URL the form posts to. However, you can do a redirect immediately after processing the data of the post to the URL you want the user to be at.
Alternately, you can change your form actions so they all post to the same page and just allow for multiple types of posting in your PHP.
You can use Ajax and pass data from form to php script and than load new form. Or you can pass argument(hidden or not) to file:
<form action="form.php?p=1" method="post">
...
</form>
Hidden parameter:
<form action="form.php" method="post">
<input type="hidden" value="4" name="p">
...
</form>
Depending on p value you load proper form and do things with rest of data.
Load your php site inside an iframe within some page.. The url of this page will always remain.
think i found another solution and want to share it:
the solution with the switch and the solution with ajax made me think about how to solve this easier. why not make the form on one page and hide the different parts. what waas before a button to submit the part is now a button to hide previous part and show next.
<script type="text/javascript">
window.addEvent('domready', function()
{
$$('.blau').set('text', 'Erkennen Sie die Heilpflanze?');
$('page_2').slide('hide');
$('page_3').slide('hide');
$('page_4').slide('hide');
$('page_5').slide('hide');
var togglePrefix = 'toggle_', boxPrefix = 'page_', emptyPrefix = '';
$$('.submit_box a').addEvent('click', function(e)
{
e.stop();
var id = $(this.get('id').replace(togglePrefix,emptyPrefix));
var id_new = parseInt($(this).get('id').replace(togglePrefix, emptyPrefix)) + 1;
var next = ('page_'+id_new);
var id_old = $(this.get('id').replace(togglePrefix,boxPrefix));
$(id_old).set('slide', {duration: 'long', transition: 'linear'});
$(id_old).slide('out');
$(next).slide('in');
if (next == 'page_1')
{
}
if (next == 'page_2')
{
}
if (next == 'page_3')
{
}
if (next == 'page_4')
{
$$('.blau').set('text', '....und die letzte ist?');
}
if (next == 'page_5')
{
$$('.blau').set('text', 'Nur noch ein paar Daten:');
}
});
});
</script>
the html:
<form id="gewinnspiel" name="gewinnspiel" method="post" action="<?=$_SERVER[PHP_SELF]; ?>">
<div id="page_1">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage1" value="Kamille" /><span>Kamille</span></div>
<div><input type="radio" name="frage1" value="Kaktus" /><span>Kaktus</span></div>
<div><input type="radio" name="frage1" value="Krokus" /><span>Krokus</span></div>
</div>
<div class="submit_box"><a id="toggle_1" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_2">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage2" value="Ringelblume" /><span>Ringelblume</span></div>
<div><input type="radio" name="frage2" value="Rotklee" /><span>Rotklee</span></div>
<div><input type="radio" name="frage2" value="Ringelkraut" /><span>Ringelkraut</span></div>
</div>
<div class="submit_box"><a id="toggle_2" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_3">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage3" value="Enzian" /><span>Enzian</span></div>
<div><input type="radio" name="frage3" value="Eisenkraut" /><span>Eisenkraut</span></div>
<div><input type="radio" name="frage3" value="Eiche" /><span>Eiche</span></div>
</div>
<div class="submit_box"><a id="toggle_3" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_4">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box">
<div><input type="radio" name="frage4" value="Wollblume" /><span>Wollblume</span></div>
<div><input type="radio" name="frage4" value="Tulpe" /><span>Tulpe</span></div>
<div><input type="radio" name="frage4" value="Rose" /><span>Rose</span></div>
</div>
<div class="submit_box"><a id="toggle_4" class="frage">nächste Frage...</a></div>
</div>
<div class="gewinn_werbung"></div>
</div>
</div>
<div id="page_5">
<div class="inhalt-gewinn">
<div class="gewinn_bild"></div>
<div class="gewinn_form">
<div class="input_box_ende">
<?php echo '<input name="date" type="hidden" value="', time(), '" />'; ?>
<div class="nosee">eMail:<input name="email" type="text" id="email" value="<?=$_POST['email']; ?>" size="30" /></div>
<div><span>Vorname:</span><input type="text" name="vorname" value="<?=$_POST['vorname']; ?>"/></div>
<div><span>Nachname:</span><input type="text" name="nachname" value="<?=$_POST['nachname']; ?>"/></div>
<div><span>Strasse:</span><input type="text" name="strasse" value="<?=$_POST['strasse']; ?>"/></div>
<div><span>Ort:</span><input type="text" name="ort" value="<?=$_POST['ort']; ?>"/></div>
<div><span>PLZ:</span><input type="text" name="plz" value="<?=$_POST['plz']; ?>"/></div>
<div><span>eMail:</span><input type="text" name="imehl" value="<?=$_POST['imehl']; ?>"/></div>
</div>
<div class="submit_box"><input id="submit" name="senden" type="submit" value="Senden" /></div>
</form>
</div>
</div>
</div>
all working ! thx for the inspiration!