I am trying to show a paper-toast when the user forgets to fill in some data in a form or when he submits a wrong e-mailadres.
I have this PHP-code that will print out an error message on the screen when the user submits the form and forgets to fill in the input or when he submits a wrong e-mailadres. This works fine. Here's a small part of the code:
<?PHP
if(isset($errorMsg) && $errorMsg) {
echo "<p>*",htmlspecialchars($errorMsg),"</p>\n\n";
}
?>
I want to make it so, that the error message appears in a paper-toast. Further, i want to display a paper-toast when the form is successfully submitted.
My question is: is it possible to call a paper-toast with the error message and appears when the form is submitted?
Thank you in advance,
The only way PHP can "trigger client side events" is by outputting HTML that will behave as you want. In your case, you basically need to output the HTML for a toast and make sure it opens as soon as the page is loaded. To do that, just set the opened attribute:
printf('<paper-toast text="%s" opened></paper-toast>', htmlspecialchars($errorMsg));
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
First question here, so please be patient with me.
I am developing a website for my company. I have an HTML form that uses a php file to email me the data entered by the user. But the user is routed to an ugly .php page with a simple "Thank you" message.
My question has several parts.
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
Considering question 1
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
It is not mandatory to have a separate page to display response to the end user. Actually it depends on the scenario for which you are developing the page. The main intention is to provide the best UI experience for the end users / visitors of your site. If there is some crucial information with large content, then it is better to show it in another page after form submission. If it is just a success message then it is fine to stay on the same page after form submit and show the response message there.
Now coming to question 2 and 3
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
You can accomplish it using ajax. With the help of jquery you can make it much simpler using
$.ajax function
Ex :
$("#formbutton").click(function(){
$.ajax({
type: "POST", // method
url: "savecontact.php", // call to external php file
data: {name:n,contact:c}, // passing variables to php file
success: function(result) {
// get the response after ajax call is successful
},
complete: function(result){
}
});
});
Now in savecontact.php you write the necessary code (usual php code to insert into table) to save the data and return response.
Ex:
result = mysqli_query("INSERT INTO table (NAME ) VALUES ('val')"));
if($result)
{
echo "Success";
exit;
}
else
{
echo "Error";
exit;
}
After ajax call gets executed, inside success function of ajax call you will get the response either Success or Error and based on that you can display the message into an empty div which normally placed above the form.
success: function (response) {
if(response == "Success"){
$("#div").html("Thanks, your form was submitted");
}
else{
$("#div").html("Issue with form submission.Please try again");
}
}
The appropriate message will gets display after form submit as ajax response.
Is it standard to have a "Successful Submission" page which looks nice and fancy, or is it fine to just stay on the page the form is on?
Answer: It will be good if you show Thank you!!! message on same page if the page is having other content also.
If it's fine to stay, how do I execute the php code to send the data without leaving the page?
Answer: You can use AJAX to send async request to server with data filled in form.
3.Further, if I stay on the page, how do I display a message over it saying something like "Thanks, your form was submitted"?
Answer: Once you get success response from AJAX you can change the HTML code with Thank you message, and if you get some error while submitting form, you can show that error on same page without refreshing page.
It usually is based on your preferences.
For me, I usually include my validation script on the action page (Redirects to a blank page temporarily). I save my post variables into my session so I can reload the form later. If there is an error, I save the error message in a session variable then redirect to my form page. In my form, I include in each input element this fragment
value=<?php if isset($_SESSION['var_name']){echo $_SESSION['var_name'];}?>
If the data is valid however, I will execute certain stuff (database inserts, mailing, etc...) before redirecting to my landing page (usually the homepage).
As to properly answer your 3 questions:
Like I said, it is preferential. It can range from a simple pop-up to redirect,to a full fledged page. However, it IS standard to inform your user about the submission's status. He won't be doing guess work on your page.
If you want to stay on your form page, try to send your form through AJAX to your validation script. Nice and simple
Refer to #2. You execute your code in the AJAX snippet
Yes you can use same page which your going to submit form.
You can set action to another .php page as example sendEmail.php
action="sendEmail.php" method="post"
and you can get form submitting value in there. when the email sent you can put condition like this
if(emailSent == true){
header('Location: http://www.example.com/index.php?success=true');
}
when the sendEmail.php redirect to the index page you can get url value using php. And display the success message
$paraVal = $_GET["success"];
if($paraVal == true){
// display message
}
I have a questionnaire in a form. In the end of it the submit button is pressed that is supposed to call a .php file that inserts data into a database through its action information and afterwards show the last page that contains something like "thank you for participating etc." via the onsubmit info.
problem is that the last page is shown before the .php file is shown which means it is visible only for like half a second and then the php script is carried out which ends up showing a blank page.
The php script works it inserts data into the questionnaire correctly so there should be no mistakes syntax-wise.
any ideas if I have to exit the cript or something and return to the .html file or what could be wrong?
on your opening form tag add action="submit.php"
then once it goes to that page when the submit button is hit add this to the bottom of that php page:
header("Location: successfull.html");
IT sounds like what youre doing is showing the message with Javascript via the onsubmit event - this happens before the request is even set to the server and the php script. Youd either need to do an ajax form submission and then display the message when the request completes or make the php script redirect to the success message page when it is done.
But this is all just speculation without seeing any code... you should post some :-)
Why not submit the form to process.php then process it:
if(isset($_POST)){
$name = $_POST['name'];
// etc etc
// if all error checks pass, then echo out - thanks for taking part in our survey!
}
What you're doing is submitting it, and it seems you're getting javascript to say 'thank you' but it is being submitted before this thank you message can be displayed - no harm in echoing this out on your .php page!!
Update
You mention about redirecting to a page afterwards, but this can be done by:
header("Location: where/to/go.php");
exit;
But you can't do this with the above (echoing out a success) since it will redirect straight away.
The way I deal with this is putting the html contents into the php file.
<?php
if (!isset($_POST["submit"])) { // if page is not submitted to itself echo the form
?>
<html>
<head>
<title>survey</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
...... (your form) ......
<input type="submit" value="submit" name="submit"><br />
</form><br />
</body>
</html>
<?
}
else {
$db = new PDO('...');
$db->exec(...);
echo "Thank you!";
}
?>
A few ways you could accomplish that.
You could make the php file it submits send out the data for the "thank you for participating" page (if you're fine with simply going to another page).
Alternatively, if you want to stay on the same page but just show the "thank you" notification, I would use JavaScript to disable the default action (e.preventDefault(); in the event handler) for the "submit" button on the forum, then also use JavaScript to use AJAX to submit the data.
An example (using JQuery), which won't change your page and perform the submit in the background, and display the "thank you" when done, on the current page.
$("a#confirmSubmit").click(function(e) {
e.preventDefault(); // Prevents the submit button from changing pages
data = {
Name: $("input#Name").attr("value")
// Add other data here also
};
$.post("/page/to/submit/to.php", data, function(d) {
//Write the code here to show the "thank you" notification.
//It will show upon completion here.
});
});
If you want to check for errors with inserting into the DB, you could check the value of the data of the AJAX call, to conditionally show the error. You can then return the user to the exact same form they were already on, with all the data still there, and show them an error message.
i am using this code to print empty filed error
if(isset($_POST['submit'])){
$oth1inp= new CheckInputFieldsAll();
$oth1inp->other1=$_POST['other'];
echo $oth1inp->chkInputOtherOne();
}
this code is on page B, to print error when i submit page B.
But when i go from page A to B it prints the error.
as the page opens error message shows while it should show when i submit page B.
This question also sets some context:
how to stop my php page from continuing when field is empty
If I understand the problem correctly you need some way of identifying whether it's pageA or pageB POSTing data to pageB.
You could include a hidden form input in the forms of both pages and check for it in the $_POST array as you're checking for the submit variable at the moment.
Another way of doing it would be to change the name of the submit button so that in the form on pageA it's called "submitA" and from pageB it's called "submitB"
This is the whole source code, i would like to modify it to add a new column to show the
Client Mobile
Client Office Telephone
Client E-mail
in an another popup php pages.
What i have attempted is to add a form and a submit button to create a new column , when i press that submit button, the mobile, office,email information will post to another php page to print out. however, since there is a another form exist already, when i add a form ,the function in the following form will not work. (Don't worries , i will indicate where the problems happen.)
it is a complicated question. Thanks in advance
The source code is here:
https://docs.google.com/leaf?id=0B196kQ-9lu50OTI1NDZkMjktNzAzNi00MmM0LWIzMjgtNTQxMTIyZmYyM2I1&hl=en_US
the problem is at line 99
p.s. I just found out the form method can not get my job done, since it can not create a popup window for the information at the same time.
Looks like at line 126 there is an extra </form> tag. Delete it...that might help.
EDIT:
Ok. So it needs to look something like this:
$_SESSION['echoable'] = $aVar; //put any variable you want here, such as POST data
Then, in the popup window, write:
EDIT: forgot to say, add this to the TOP of your page.
session_start();
Then do this stuff later on.
if((isset($_SESSION['echoable'])) && (!empty($_SESSION['echoable']))) {
echo $_SESSION['echoable'];
}
else {
echo "whatever"; // whatever you want here
}
The above will print whatever var you want. Hopefully this helps...if not let me know.
I've got this registration form: http://www.topgamedb.com/register
If you enter a captcha (the wrong or the right one, it does not display a message next to it)
If you click Register, it does not display errors, it submits the form.
http://www.topgamedb.com/css/registration.css
http://www.topgamedb.com/js/registration.js
I have this working on another website, but after copying it over to this new website, I can't seem to get it to work. I'm obviously missiong something 'obvious' - its late and I haven't slept and I'll probably kick myself when I see whats wrong.
Any help is greatly appreciated.
Number 1, firebug is giving me an error that jQuery.md5 doesn't exist. Do you have another library that you added on to make this work?
You need to prevent the default behavior of the button (submitting the form):
$(register_button).click(function () {
// do validation, etc.
// submit form using $(your_form).submit();
// if values are passing validation
// prevent the form from being submitted immediately
return false;
});