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"
Related
I've create a project in PHP for my class and it is to replicate the google signup pages and when the information are correctly entered it writes the user's info in a file called "users.txt". Here is the github link to the source files : https://github.com/Ryan30012/myProject
The first page is the index page, everything works great, but to submit the information you have to click the next button two times. Then comes the page with the error which is the "process.php". If you leave everything empty and you click on submit button it should output an error about some empty field, but instead nothing happens because there is a warning that appeared in the browser console which is this:
Thank you for your help!
I figured what was the problem. It what in condition of the form:
The variable $error was not define so I've just added the line:
$error = true;
After declaring all the other variables. Now everything works.
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));
I have a form that is shown inside a Colorbox. When users click submit there is some validation done (checking if an entered field already exists in my database) if it does a message is shown and user is prompted to enter a new value. However when this happens the form is not shown the second time inside the Colorbox window, instead it appears on a blank page.
the form is posted to PHP_SELF, how can i change this to show in PHP_SELF (in the current Colorbox)?
cheers
I know this is aside from what you are asking, but
What if you used jquery's submit() and post() instead of PHP_SELF to pass form data to an external php class and handle displaying the return data? If the data is acceptable, you can call $.colorbox.close() manually. If data is unacceptable you can display a message to the user describing the problem.
$('myform').submit(function(){
//validate the data with javascript
//send the data to your function with post
$.post('http://url/to/your/function',{a:first_input,b:second_input},
function(return_data){
if(return_data == 'success'){
$.colorbox.close();
}else{
//display your error message here
}
,html
);
});
I have a form in page1.php which directs to page2.php from where the data from the form in page1.php is inserted into database. after successful insertion, page2.php displays a message and gives link to go to a third page.
The problem is when the user after the insertion hits the back button of the browser and clicks the form submit button, insertion is made again.
Is there any way so that after one insertion when the back button is pressed a message will be displayed showing that visiting the back-button is not allowed? Or in case it is allowed no insertion will take place on clicking the form submit button?
EDITED LATER TO ADD THIS PART:
okk let me tell in details. it is about an admin back end. the admin gives description text input for different products. He gives the input from page1.php and message is shown on page2.php that the description has been inserted into db. then there is another form below the message. It just asks whether the admin wishes to do more with description text. If yes, then clicking on the form submit button , he is taken to a page from where across some page(s) he is again taken to page1.php ( this time for another product), from there to page2.php and so on. btw i could use a normal page link instead of the form button link below the message on page2.php
The problem is, while the admin is on page2.php and hits the back button , he goes back to page1.php and from there if he hits on the form submit button, the data is inserted for the second time in a new row.
u may suggest to use 'IGNORE' with 'INSERT' command, but there are other columns in the row which may not get matched with those of another column while the description columns(admin inserts text data for this column ) may get matched.
1)Is ignore applicable in this case?
2)What should be the solution if duplicate entry is allowed for the database of the site?
thanks
hope it makes the whole thing clear.
Instead of giving link to a third page, redirect to the very same URI
this is quite handy method called POST/Redirect/GET:
here is a concise example of it:
<?php
if ($_SERVER['REQUEST_METHOD']=='POST') {
$err = array();
//performing all validations and raising corresponding errors
if (empty($_POST['name'])) $err[] = "Username field is required";
if (empty($_POST['text'])) $err[] = "Comments field is required";
if (!$err) {
//if no errors - saving data and redirect
header("Location: ".$_SERVER['PHP_SELF']);
exit;
} else {
// all field values should be escaped according to HTML standard
foreach ($_POST as $key => $val) {
$form[$key] = htmlspecialchars($val);
}
}
} else {
$form['name'] = $form['comments'] = '';
}
include 'form.tpl.php';
?>
Here you can see another example, concise yet powerful:
Separating Logic/Style in PHP properly
it's complete solution to display, add and edit database contents, exactly for admin purpose.
You should not display your message on page2.php.
Instead :
page2.php should deal with the data
when the data has been saved, page2.php should redirect to confirmation.php
And it's only confirmation.php which would display the message.
For more informations, take a look at the Post/Redirect/Get pattern.
Edit after the comment : but note that, in any case, you will never be able to prevent the user from re-submitting a form, if he really wants to...
The only solution you'll have is, when a form is submitted, to check in your database if the currently submitted data already exists -- and if it does, refuse to insert it again.
Of course, if the suer changes even a single letter in his input, it won't be the same data anymore...
im using a form in php to submit some information into my database
so i used two function to do this
but how to show the result in th same page that has the form
To load the same page you have to assign the variable $_SERVER[PHP_SELF] for the form action field.
<form action='$_SERVER[PHP_SELF]?op=ban' method='post'>
then when the page get load you just check the post variable ,if it contains the appropriate data then print the result with the form.(Normally people using div tag to print the results )
It's as easy as this:
if (isset($_POST['submit']))
{
// do something with your data
}
form();
Forgive me if I am wrong. I think you have copied the code from some where and using it without understanding how forms work.
<form action='index.php?op=ban' method='post'>
The above code says to which page the values should be submitted. As you can see above the values in the form will be submitted to index.php. So the DB operations will(should) happen in index.php and the Thank you message can be shown in index.php.
If you want to show your result in the same page then you will have to submit to the page in which the form resides. But in this case you should have a logic in the page to decide whether the form was submitted or was it loaded first time.
The code snippet in your question does not tell us name of the file the code exists so we wont be able to tell you whether the result will be shown in the same page. Aslo the source code is not complete.
Post a detailed source code and we will be able to help. Hope it helps.
it should be shown on the next request.
because your app should perform an HTTP redirect after POST request.
it can be same page though