Please help me with file upload with text fields details going to database in codeigniter. ( for example i want image name and some another form field going to database and file uploads to server)
Also how do i stop the form submission in database upon page resfresh ?
The best I can do is link you to the relevant pages so that you can learn more. If you provide more information I might be able to help you more:
File upload:
http://codeigniter.com/user_guide/libraries/file_uploading.html
Database:
http://codeigniter.com/user_guide/database/index.html
I recommend using the Active Record part of the database library:
http://codeigniter.com/user_guide/database/active_record.html
As for how to stop form submission on page refresh, simply use redirect('controller/method'); after you have handled the form data. For example:
if(!is_bool($this->input->post('fieldvalue'))
{
$this->model->writeToDB($_POST);
redirect('controller/method');
}
so that every time data is submitted, it is added to the db and then the redirect will revisit the page, thus the browser won't remember what was in the post array and that will solve the problem.
view:
<form method="post" action="#">
<input type="text" name="foo"/>
<input type="submit"/>
</form>
controller:
$this->load->model('yourmodel','model',true);
if(isset($_POST['text']))
$result = $this->model->doQuery($_POST['text']);
model:
function doQuery($text){
$result = $this->db->query("insert into table $text");
return $result;
}
(edit) upload:
use $_FILES and move_uploaded file
Related
I need to simply get a search term from a form into a variable. I have set up a basic form within a template file, that template file is then linked to a page within Wordpress admin. The problem I am getting is that the form doesn't submit so I am unable to use the variable. If I remove get_header(); from the template then the form will submit but obviously it break wordpress stuff.
Here is my form:
<form action="<?php the_permalink(); ?>" method="post" autocomplete="off" >
<label>
<input placeholder="Search…" name="qcsearch" type="text">
</label>
<input type="submit" name="submit" value="Submit">
</ul>
</form>
I have tried leaving out the action, using the template name which is qccerts.php and using $_SERVER['PHP_SELF']
Here is then what I am trying to do with the output:
if(isset($_POST['submit'])){
$searchterm = $_POST["qcsearch"];
}else{
$searchterm = '';
}
Its basically a simple search which tells the users if there is a file by the name they search. So I need to populate $searchterm so I can use it later down the page.
Any help appreciated.
It's difficult to determine what your exact problem is without a reproducible scenario. For example, without seeing your problem, I'm not sure whether the form is really not submitted at all, or submitted, but you did not see it being executed, or there is some Javascript which prevents your form from submitting. There is a possibility that the form is submitted to the wrong action as well.
However, if you intend to keep your search term accross the pages, you could add it into session. Let's imagine these functions:
function storeSearchTerm($searchTerm) {
$_SESSION["searchterm"] = $searchTerm;
}
function getSearchTerm() {
return isset($_SESSION["searchterm"]) ? $_SESSION["searchterm"] : "";
}
By calling these functions you can manage the search term, initializing it via storeSearchTerm($_POST["qcsearch"]) or something.
As about your actual form, if it does not work, then you can submit the form in Javascript, such as
document.getElementById("myForm").submit();
and make sure that this is triggered either via an onclick attribute, or a click event listener on the button created via addEventListener.
EDIT
It turns out that a class name was not well formed (case-sensitivity issue).
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.
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.
$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";
}
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.
I'm building a page that has a form that's submitting to my database. The form needs to post to the current page to process, but since I'm using this same form on many pages, I would like a way to tell the form to use the user's current page to submit to. I know the code below can get this information, it's what I'm using to send url data to my database for another use.
$_SERVER['REQUEST_URI']
I'm just not sure how to use that within the code that loads the page. I suppose I could write something that will pull out the submitted url info that was just submitted to my database and then plug it in, but is there a way to just use the above code to do it? That seems easier, but when I tried plugging it in, it fails and just gives me a "404 not found" error, so I know I'm not doing it correctly.
It just needs to go in the simple form post action code below:
<form action="comment_box.php" method="post">
I've looked up similar questions, but I couldn't find anything that worked with what I needed, I just got more 404 errors. Any advice would be great, thanks!
If your posting to the same page the user is on you can do
<form action="" method="post">
Leaving the form action blank it will post to what ever the page the form is on.
Not sure what language your using but I assume php you then use
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
// form submit button was pressed your php code here
} else {
// no form submit button was press do nothing maybe show form html code here
}
?>
or what your trying to achieve
$url = $_SERVER['REQUEST_URI'];
Then in PHP
<form action="<?php echo $url; ?>/comment_box.php" method="post">
for example you have a following code
<input type="submit" name="click_me" value="Login">
then in the php, you have to check like
<?php
if(isset($_POST,$_POST['click_me']) && $_POST['click_me']="Login"){
//do your stuff
}
?>
In my create page, i am using one or more file upload text box , select box ,text box inside a form , After filling all form datas and submit, I will do a server validation then insert into db...
Before insert into db, if server validation fails, filled datas are lossed , Is there any easy way to prevent data loss?
for select and text box we can pass the value of $_POST
But for file upload text box , how to prevent data loss? Can we store in session?
Thanks in advance..
well you can use some variables to store the data, then you can perform the validation
well i am giving a example of php validation
<?php
if(isset($_POST['submit'])){
$name = $_POST['name']; //
// and some other data input
and goes the validation part
?>
<form ... >
<input type="text" name="name" value="<?php if(isset($name)) echo $name; ?>"
</form>
well.. this must work..
you might find other better ways.. but this will also work
As per my knowledge you can not maintain the file upload values as like the other form elements.
If you still want to maintain the $_FILES values. You can take them in session.
$_SESSION['imagename'] = $_FILES['image']['name'];
$_SESSION['imagetmp'] = $_FILES['image']['tmp_name'];
You can save the data in variables and populate it later, except for the type="file" input field. It will be a security problem if the files can be prepopulated(or say selected without user permission) and browsers won't allow it.
One option is to save the file in any temporary folder in server and if validations worked fine, move it to the uploads directory.
Another option is to show your form in steps(in pages) and the file upload option in the last step, so that the user will reach the file upload section after all validations and you can do the file upload validation in the last step.
My Code :
<?php
function dbAdd($first_name , $image) {
//mysql connect database code...
mysql_query("INSERT INTO users SET first_name = '".$first_name."', image = '".$image."'");
$mysql_close($sql);
}
if($_SERVER['REQUEST_METHOD']=='POST') {
dbAdd($_POST['first_name'], $_POST['image']);
}
?>
<form enctype="multipart/form-data" method="post" action="">
First Name : <input type="text" name="first_name" >
Image : <input type="file" name="image">
<input type="submit">
</form>
The form "file" is to upload. I know that. But I wonder how to get the values so I can put the path of image in the database. The code is already working. The $first_name can already save to the database.
Thank you for the answers.
Jordan Pagaduan
The file will be uploaded to a temporary place on the server when the form is submitted.
Once the form has been submitted, the $_FILES variable will contain all the files submitted. In your case, you could access the uploaded file using $_FILES['image']. Most likely you will want to move the file out of the temporary directory to a safer place.
For more info, have a look at the PHP manual on the topic, specifically the page on handling POST uploads. That second page has an example for you on how to move the uploaded file (have a look at the move_uploaded_file() method).
Straight from W3C: Upload form & $_FILE variable