-2

I have a html page where the user can input some text, it is then posted to a php file and then stored in a database.

<form action="postphp.php" method="post" enctype="multipart/form-data">
         <center><input id="postTitleStyle"  type="text" name="title" size="100" maxlength = "180" ></center> 
         <center><textarea id="postTextStyle" name="Text" rows="10"cols="96" maxlength = "40000"><?php echo $text;?></textarea></center>
         <center><input id="postTagStyle"  type="text" name="tags" size="100" maxlength = "900" ></center> 
 <center><input type="submit" class = "Post2" value="post"></center>

     </form>

Above is my current code for posting the data in the text field to a php file. I want to be able to click a button that when clicked will not go to the php file it will be stored and then when the user clicks the submit button it is posted. For example the user clicks it, a one is stored and then sent later when the user clicks the submit button after they are finished filling in other details. Is this possible?

P.S. I want to avoid Javascipt as much as possible for the moment, so if there is a non-java way of doing it then it would be much appreciated.

Many thanks, Jack.

Jack
  • 23
  • 1
  • 4
  • I am not sure I am following. What do you mean with " to click a button that when clicked will not go to the php file it will be stored". So what should be stored and where? – jakub wrona Sep 25 '16 at 14:00

3 Answers3

1

There are two easy solutions to this problem without using Javascript. I'm assuming by your wording that you can currently post a form, but you don't know how to do so without leaving the current page. That's what I'll be answering below, but please note that there is no way to post a form without reloading at all without Javascript.

Solution 1: Put the PHP code into the same page the form is on and change the form tag to: <form action="" method="post" enctype="multipart/form-data">

A blank action field will cause it to run the PHP on the current page. You will likely need to look into using isset($_POST['submit']) in PHP, which will check whether the submit button has been clicked on before running that particular PHP code. You can read more about that HERE.

Solution 2:

In the postphp.php file that's currently linked to in your action field of your form, you could use a PHP header that will redirect the user after the PHP code is ran.

E.g.

<?php
{All your form processing code goes here}
header('location: my_form_page.php');
?>

In this example, my_form_page.php is the page on which your form is on. You can read more about headers in the answer of THIS question.

Hopefully this helps a bit.

Community
  • 1
  • 1
PL200
  • 741
  • 6
  • 24
0
$title = $_POST['title'];
$text= $_POST['text'];
$tags = $_POST['tags'];

mysql_query("INSERT INTO `table_name` (`colname1`,`colname2`,`colname3`) VALUES ('$title,'$text','$tags')");
   $id = mysql_insert_id();
if($id){
echo "inserted";
}else{
echo "Not inserted";
}
Ankneema
  • 45
  • 5
0

For this you need to use Ajax (JavaScript will be used) because you need a button which send data to server without form submission and page reload it can be easily achieved using Ajax.

Arpit Kumar
  • 2,179
  • 5
  • 28
  • 53