I have this submit form of PHP. Then when i submit it --it goes to my database.
It does save, but i need to refresh my page to see the data that has been save to my DB.
My Page -> Submit DB -> My Page
Sort of like that, so i in the save page when i do the submit function.
Why do i need to refresh my page to see the result of the data that has been saved?
Or do i need to create a new page? or do i need to add some queries on url?
Thank you.
UPDATED:
Thank you for all the help guys.
I just used the isset, and its working now
Probably you're querying data first and then you're saving form to database. Check it and change sequence if it is true.
Assuming your Submit DB code is on same My Page.
At the start of page check if submit button is clicked
<?php
if(isset($_POST['submit']))
{
//your insert query
}
?>
In your body tag
<?php
// display your table
?>
You should use a single page both displaying, inserting and reading from/into database
<?php
if (isset($_POST['insert']) && $_POST['insert'] == "yes")
{
//run your insert query
}
?>
<!-- put your form here -->
remember to add the code below into your form
<input type="hidden" name="insert" value="yes" />
<!-- put your table here -->
Related
I have a form where the user will input a bunch of data. It is set up like this:
<form action="add.php" method="post">
This makes it so that when I click my submit button it will go to the add.php page. add.php however is a blank page because it only runs some SQL queries and does nothing else.
I want it so that when I click submit, the information is sent one place and the screen goes to another. My button is set up like this:
<input type="submit" value="Submit">
Just to make sure I am explaining the issue correctly, when I click submit I want the data from the form to go to add.php, and the browser to display a different page rather than displaying the add.php page, which is what it is doing right now.
The solution is easy, send data to add.php, in the end of your process. redirect to another page, let's say page.php
on your add.php
<?php
/// do you mysql stuffs here
header('Location: page.php');
?>
Here is two solutions :
Asynchronous solution
Use Javascript and ajax to send the data to add.php asynchronously and redirect the user to the page you want.
This solution might be not a good one, because the process result of add.php is ignored (if you redirect the user before getting the result)
Synchronous solution
Redirect the user to add.php, then, redirect the user to the page you want at the end of the process.
After your SQL Queries are done on add.php you can use the header function and pass your data via get-method:
header("Location: nextSite.php?key=value"); //Replace key and value with whatever you want
we can use a session or cookie in PHP.
Session:
//On page 1
$_SESSION['name'] = $formvariable; // You can use document.getElementById('form input
name')
//On page 2
$var_value = $_SESSION['name'];
Using include method
//On page 1
This page has a form without submit button
//On page 2
This page has a submit button
so we include page 1 something like this.
EX:
vars.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
I hope this will help you to solve your issue- thanks!
I have a form containing a textarea for inputing text into. The form also contains a submit button. After pressing the submit button it posts the text within the textarea into my php document. Within my php document the text is added to a database. Once it has been added to the database I would like it to echo back a response telling the user that it has added the text to the database successfully.
However, if i make it echo that response back to the home page, there is nowhere declared for it to display the echoed message. Has anyone got an idea of what i should be doing in order to get this working? Many Thanks.
Normally i wouldn't use a post straight from the form and i would use ajax and then display the data within a paragraph or something on it's return, however since the form is doing the post it's self i am not sure where to then declare where the response should show up.
The bellow displays my html form code and shows it's action to post to a php file.
<div id="userban2"><form id="bannable" action="/onlineusers.php" method="post"><p> Type username to ban bellow:</p>
<textarea name="banned" id="banned" maxlength="255"></textarea><br/>
<input type="submit" value="Send" class="extrabuttons" onclick="return false; preventDefault();">
<div id="cancelban" class="extrabuttons"><p> cancel</p></div>
</form>
However when in my php file i write ....
echo "the information has been added to the database successfully";
It might send the echo back however it isn't declared to display anywhere how can i change this to make it display the response within my form?
As requested return from my php
if(isset($_POST["banned"])){
$ban_name = $_POST["banned"];
bannedd($ban_name);
}
function bannedd($ban_name) {
$query1 = mysql_query("INSERT INTO banned_users (username,firstname,lastname,email,password,ip_address,sign_up_date,last_logged_in,about,sta rr,userpref) VALUES('$usernameb','$fnameb','$lnameb','$emailb','$passwordb','$ip_addressb','$sign_up_date b','$last_logged_inb','$aboutb','$starrb','$userprefb')") or die("Could not insert your informaion");
echo "This user has successfully been banned";
}
The form posts what is written in the form due to it having the action and method of post to my php. However should i then have any return i am not sure how i declare where the returned information should then show (The echoed message).
If I understand you correctly, your form is in some index.php file and sends the data to other file - onlineusers.php, and you want to display the message in the original page?
If this is the case, the most simple way I can think of is redirect back to the original page with a URL parameter, instead of echoing.
Do this at the end of onlineusers.php:
<?php
// insert text into DB ...
header("Location: index.php?result=ok");
?>
This redirects the browser back to the original page with the form. There you check if the status variable is set:
<html>
<head></head>
<body>
<?php if(isset($_GET["result"]) && $_GET["result"]=="ok") { ?>
<p>The information has been added to the database successfully</p>
<?php } ?>
<form> ... </form>
</body>
</html>
As you can probably see, you could set other results, such as "error" this way.
If you don't like the extra string in your URL, then create a cookie after processing the form in onlineusers.php and back at the original page, check if such cookie has been set. If you need more detail on that, let me know. And if you're asking something completely different, well, never mind :)
Your form is being submitted to /onlineusers.php
This is where you would want to add your echo statement.
If you require the info on the same page you technically return to the same page with the form action being $_SERVER['PHP_SELF'].
<form id="bannable" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Then you can put in a conditional statement prior to the load of your document, and include the PHP script.
<?php
$testVar = false;
$msg = '';
if($_POST) {
include '/onlineusers.php';
//... do something e.g post to database and return true.
}
if($testVar) {
$msg = 'Successful writing to DB!';
} ?>
<html>
<body>
<?php echo $msg; ?>
</body>
</html>
This will check to see if you have any post data, if you do, then it includes the script you specify. Maybe set $testVar to true if the writing to DB is successful, and then return $msg in your HTML.
I am having difficulty making a button that removes existing values in MySQL database field. I have read up some information on it, some say use AJAX, but i'm not sure. What are your opinions? Can any Stackoverflow-er show me a method? I recognise I cannot use Javascript as it is on the client side and not server-side - but is there another way?
create form first in html
<form method="get" action="delete.php">
Enter id you want to delete : <input type="text" name="id">
<input type="submit">
</form>
delete.php
<?php
$dd=$_GET["id"];
$c=mysql_connect("localhost","root","");
mysql_select_db("mydatabase")
mysql_query("delete form mytable where id=$dd");
print "Remove successfully...";
?>
This upper code will remove the entries you want on clicking of submit button
If you want to remove the values from mysql database asynchronously then you'll need to use AJAX and that's the only option else if you are allowed to do it without refreshing the page then go with simple PHP.
you need not to do it with ajax if u dont want. simple php is good enuf..
just make a form with a button field.
post a variable though it to your php.
connect with your database using update the dtabase as you want..
Beware : One refresh would take place on this process as its not an asynchronous process. else you can choose AJAX
you could use a (post/get) request to a file on your server but i recommend you use AJAX to do this, the server side file will still look pretty much the same.
if you are using php for the back end the code would look like
$mysqli = new mysqli("localhost", "user", "password", "database");
$res = $mysqli->query('your delete statement');
if(! $mysqli->errorno )
header('Location:success url');
else
header('Location:fail url?error=' . $mysqli->error')
you could use a link for you button
<a class="button" href="script to delete">delete unwanted data</a>
I am trying to submit a form using a hyperlink and it is not posting values onto the next page.Here is my code form:
<?php
$email = array('name'=>'accountno','id'=>'accountno','value'=>set_value('email'));
?>
<form method="post" id = "login_form" action="/salesrep/check" name = "login_form" class="custLogin">
<fieldset style="color: #BD1313; width: 440px;"> <input type="hidden" name="submit_type" id="submit_type" value="account_only">
<br><center><label for="customerNo"><b>Customer No:</b></label>
<? echo form_input($email);?>
Submit<? echo form_input($button);?> </center>
<p> </p>
</fieldset>
</form>
The code on the next page looks like this:
<?
print_array($_POST);
die();
?>
When i use the button here,it posts values to next page successfully.BUT I HAVE not been able to post values using the hyperlink on onclick event. Where i am making mistake??
Why i am getting empty array when i am already inserting value in the text box.?? Or is there any way i could post values using the link and retrieve them in the next page???
The real problem is that you are trying to use a link plus some JavaScript to submit a form in the first place. Don't do this. Buttons inform users that they will submit the form. They will show up in screen readers when they are in forms mode (a link with some JavaScript won't). They "just work".
If you insist on using a link and some JavaScript, then the reason that your code doesn't work is that the JavaScript runs, the form starts to submit, then the link is followed and a GET request is made to the page instead.
Normally you could call preventDefault on the event to stop this, but you are using old style intrinsic event attributes so you need to return false; from there instead.
Recommended reading: Progressive Enhancement and Unobtrusive JavaScript
I have an index.html page, where is an html form with some fields, for example AUTHOR and TITLE. These fields are created dinamically as DOM objects. The user type datas into the form and click submit. Then index.html POST the datas to search.php. This php site searches in mysql database and writes the search results into an html table on the search.php.
I added an BACK button on the search.php. When the user clicks on the BACK button, this buton goes back to the index.html. I want to the user can see the search datas in the fields.
For example user types into the authors field: Paulo Coelho and the title field: The Alchemist. Then hits SUBMIT button and search.php searches in the database and writes out the results. After user want to back to the prevous page and hits BACK button. This goes back to index.html and here the AUTHOR field is filled with Paulo Coelho and TITLE is filled with The Alchemist. I would like to get any solution for this problem.
I have already some starting points, for example php session, cookies, or POST back search conditions to index.html. Thanks!
Have you tried window.history.back() any values typed in the input boxes will be preserved:
<input type="button" value="Back" onclick="javascript:window.history.back();" />
First of all change the index.html to index.php so you can use php code there, Than store the form values in the session and then echo the session values.
First on all start the session of both the pages..
<?php session_start(); ?>
Store the Posted values in session like this.
<?php
$_SESSION['fieldvalue1'] = $_POST['fieldvalue1'];
$_SESSION['fieldvalue2'] = $_POST['fieldvalue2'];
?>
And then echo to values in form like
<input type="text" value="<?php
if(isset($_SESSION['fieldvalue2']))
{
echo $_SESSION['fieldvalue2']
}
unset($_SESSION['fieldvalue2'])
?>" name = "fieldvalue2">