insert form to database - php

i have program codeigniter anda i want to insert form to database.
code view:
<form target="paypal" method="post">
<div class="field1">
<div class="field">
<label>Nama</label>
<input placeholder="Nama" name="nama" type="text">
</div>
<div class="field">
<label>No. HP</label>
<input placeholder="No. HP" name="handphone" type="text">
</div>
<div class="field">
<label>Alamat</label>
<input placeholder="alamat" name="alamat" type="text">
</div>
<div class="field">
<label>Jumlah</label>
<div class="selectbox">
<select name="jumlah" id="">
<?php for ($i=1; $i <= 20; $i++): ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
</div>
<button type="submit" name="submit" class="ui teal button order-button">Order now</button>
</div>
</form>
code controller
function simpanOrder()
{
$this->load->model("M_order");
$data['nama'] = $_POST['nama'];
$data['handphone'] = $_POST['handphone'];
$data['alamat'] = $_POST['alamat'];
$data['jumlah'] = $_POST['jumlah'];
if($this->input->post('submit')){
$this->M_order->insert($data);
}
}
when i click submit data not insert to database. so can you help me with this code problem? thanks.

Your form doesn't have an action, and therefore may not be going to the function you want it to. (/controller/function)
<form target="paypal" method="post">
Also, instead of using a button to submit the form - try using <input type="submit"...
Using the <button>, in some browsers, you would have "submit" submitted, in others, "Order now".
If the above doesn't work - check your SQL.
As a side note, CodeIgniter has a form helper and a form_validation library which are quite useful if you're already using CodeIgniter. That won't fix your problem but it's just something I felt I would point out.
See:
http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

Call model from controller. and write below code in model.
$data = array(
'handphone' => $this->input->post('handphone'),
'alamat' => $this->input->post('alamat'),
)
In this array key is database columnname.
$this->db->insert(yourtablname, $data);
$insert_id = $this->db->insert_id();

You need to define action attribute in form tag where you will provide controller name and method name like this
<form action="<?php echo site_url('controllername/simpanOrder')?>" method="post">
After posting you can debug your code like this
$post = $this->input->post();
echo '<pre>';
print_r($post);
Then
if($this->input->post('submit')){
$this->M_order->insert($data);
}
And finally
echo $this->db->last_query();
This will display you the last query run.

Related

Dynamic HTML form and saving into multiple records in DB

I am writing a small app using codeigniter to track payments and payments dates. I have a form where we input details about the customer, and among those fields, we enter how many instalments will be used for the customer to pay his entire premium.
The next page takes this number, and generates the same number of fields. For example instalment_1, date_1; instalment_2, date_2.
<?php for($i=1; $i<=$instalments; $i++):?>
<div class="row form-group">
<div><label>Instalment <?php echo $i;?></label></div>
<div>
<input type="text" id="instalment_<?php echo $i;?>" name="instalment_<?php echo $i;?>">
</div>
</div>
<div class="row form-group">
<div><label>Premium Date <?php echo $i;?></label></div>
<div>
<input type="text" id="date_<?php echo $i;?>" name="date_<?php echo $i;?>" class="form-control">
</div>
</div>
The idea is now to loop through this form, and save each set of instalment and date into 1 record in the database.
i was able to do it if i have only one type of fields (eg instalments). but when i added the date, it is saving, but each record gets created twice.
$post = $this->input->post();
foreach($post as $key=>$value){
//check for the word instalment
if(strpos($key, "instalment") == 0){
//get the instalment number from the name of the field
$inst_number = substr($key, -1);
//Use $category_id and $value in this loop to build update statement
echo "<script type='text/javascript'>alert('$inst_number');</script>";
//$arr = "inst_".$inst_number;
//reform the name of the field to get the values from it
$instalment_var = "instalment_".$inst_number;
$date_var = "date_".$inst_number;
echo "<script type='text/javascript'>alert('$instalment_var');</script>";
$amount = $this->input->post($instalment_var);
$date = $this->input->post($date_var);
echo "<script type='text/javascript'>alert('$amount');</script>";
$arr = array(
'payment_amount' => $amount,
'payment_date' => $date,
'policy_name' => $policy_id,
);
if(!$this->Policy_model->save_payment_details($arr)){
redirect('ships/index');
}
}
}
How can i do it so that i don't have every record saved twice? and if i add another field, it will get saved 3 times.
i though about naming the div, and working on this, but i didnt get any result.
Thank you in advance.
Try this code.This will resolve your issue.
View:
<form action="action.php" method="POST">
<?php for($i=1; $i<=$instalments; $i++){?>
<div class="row form-group">
<div><label>Instalment <?php echo $i;?></label></div>
<div>
<input type="text" id="instalment[]" name="instalment[]">
</div>
</div>
<div class="row form-group">
<div><label>Premium Date <?php echo $i;?></label></div>
<div>
<input type="text" id="date_<?php echo $i;?>" name="date[]" class="form-control">
</div>
</div>
<?php } ?>
<input type="submit" name="submit" value="submit">
</form>
In your controller:
$post = $this->input->post();
$i=0;
foreach($post as $key=>$value){
$arr = array(
'payment_amount' =>$post['instalment'][$i],
'payment_date' => $post['date'][$i],
'policy_name' => 1,
);
$i++;
}exit;

save input data in php to notepad.txt

<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="button" onclick="generateBarcode();" value=" Generate "> <input type="button" onclick="printDiv('print')" value="Print" />
</div>
</form>
<?php
$barcodeValue = $_POST["barcodeValue"];
$save = file_get_contents("save.txt");
$save = "$barcodeValue" . $save;
file_put_contents("save.txt", $save);
echo $save;
?>
sample picture
How to save the input data in save.txt file. When i clicked generate button the text file not showing in same folder.
The problem with your code is you have no submit button so your form was not actually posting when you pressed the button. if you look at my edits you can see I changed the button from input type="button" to type="submit". That allows for the form to submit back to the same php script.
Your script also was causing errors because you accessed $_POST["barcodeValue"] without checking if it existed. You also have to check if the save.txt exists before reading from it. If analyze my edits you can see how checking if the variables are available will help quite a bit.
<form id="form" name="form" method="post" action="">
<div class="jquery-script-clear"></div>
<h1>BARCODE GENERATOR</h1>
<div id="generator"> Please fill in the code :
<input type="text" name="barcodeValue" id="barcodeValue" value="1234"><br> <br>
</div>
<div id="submit">
<input type="submit" value=" Generate ">
</div>
</form>
<?php
if(isset($_POST["barcodeValue"]))
{
$barcodeValue = $_POST["barcodeValue"];
if(file_exists("save.txt"))
$save = file_get_contents("save.txt");
else
$save = "";
$save = $barcodeValue . $save;
file_put_contents("save.txt", $save);
echo $save;
}
?>
Let me know if you need more help

Submit form does not work correctly

<div class="container">
<form class="form-signin">
<h2 class="form-signin-heading">Please sign in</h2>
<?php echo form_open('login/validate_credentials');?>
<?php $u = 'placeholder="Username"';
$p = 'placeholder="Password"';?>
<?php echo form_input('username','',$u,'class="input-block-level"');?>
<?php echo form_password('password','',$p,'class="input-block-level"');?>
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<?php echo form_submit('submit','Sign in','class= "btn btn-primary"');?>
<?php echo anchor('login/signup','Sign up!', 'class= "btn btn-primary"');?>.<br/><br />
<?php echo anchor('login/admin_log','Go to admin login page');?>
<?php echo form_close();?>
</form>
</div>
I have a login form. When I click sign , it's not redirecting me to the form_open page.
you need to create an action for the form, usually a php script on a different page to handle the data:
as an example:
<form action= "../create_comment.php" method="post" name="comments_form" id="comment" enctype="multipart/form-data">
<div>
<label>Name<span>*</span></label>
<input name="name" type="text" value=" ">
</div>
</form>
You have two forms Parent and child(form inside form).
Submitting the form will process parent form. Simply remove the first (parent) <form> tag.
<form class="form-signin">
^^^^^^^^^^^^^^^^^^^^^^^^^^ ------ remove this
...
...
</form>
^^^^^^^ ------ and this
It looks like you might be using CodeIgniter?
You have 2 form tags in your code.
here: <form class="form-signin">...</form>
and here:
<?php echo form_open('login/validate_credentials');?>...<?php echo form_close();?>
Get rid of this one: <form class="form-signin">...</form>
Your second form tag will handle everything for you. The output will look something like this:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/login/validate_credentials" />
If you need to add a class or any other property to the form tag, do this:
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);
Form helper on CI Docs

How do you correctly pass post variables from the view to the controller in CodeIgniter?

I haven't used CodeIgniter in nearly a year and I seem to have forgotten a lot of the fundamentals.
I am trying to retrieve the post variables from a form and pass them into a model which inserts them into mysql.
The controller function my form submits to looks like this:
public function validation() {
$this->load->helper("form");
$this->load->model("contact_form");
$data = array(
"name" => $this->input->post("name"),
... etc. etc. ....
);
if ($this->contact_form->new_form($data)) {
$this->load->view("header");
$this->load->view("submitted");
} else echo "Sorry, there was a problem adding the form to the database.";
}
The form in the view is structured like so:
<? echo form_open("form/validation");?>
<div id="one" style="display: block;">
<h1>A Heading</h1>
<p>Some Text</p>
<p class="bold">Name: <input type="text" name="name" class="single" value="<?php echo set_value('name'); ?>"></p>
<p class="bold">Email: <input type="text" name="email" class="single" value="<?php echo set_value('email'); ?>"></p>
<p class="bold">And then some radio buttons</p>
<p> yes <input type="radio" name="registered" value="yes"> no <input type="radio" name="registered" value="no"></p>
<p class="bold">And a textarea...</p>
<textarea name="description" class="fill" value="<?php echo set_value('description'); ?>"></textarea>
next
</div>
<div id="two" style="display:none;">
<h1>Another Heading...</h1>
<p class="bold">And some more textareas</p>
<textarea name="audience" class="fill"></textarea>
... There are four divs in total with further textarea fields ...
<p class="bold"><input type="submit" name="submit" value="submit" class="center"></p>
back
</div>
<? echo form_close();?>
And finally my model is very simple:
class contact_form extends CI_Model {
public function new_form($data) {
$query = $this->db->insert("contact", $data);
if ($query) {
return true;
} else return false;
}
}
The form processes without any errors, but the data just appears as 0's in MySQL. If at any point I attempt to output the value of $_POST it returns BOOL (false), or with $this->input->post('something'); it returns NULL.
You will notice that no actual validation takes place. Initially I was using $this->form_validation->run() and getting the same results. I thought perhaps I was having trouble with the validation so I stripped it out and now I'm fairly certain my problem is that I'm not passing the $_POST variables correctly.
Can anyone explain why I am failing so hard?
I have now resolved this problem.
For some reason <? echo form_open("form/validation");?> was implementing GET and not POST. Replacing that line with <form method="post" accept-charset="utf-8" action="form/validation"/> resolved the issue.
According to the CodeIgniter documentation, by default, form_open should use POST - I have no idea why in my case it decided to use GET.

submit form to page and depending on input show different div

I Have a form which when submitted needs to go to the page and then show one of 4 hidden divs depending on the page.
Here is the form
<form>
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Here is the page
<div id="brighton">
<p>Brighton</p>
</div>
<div id="devon">
<p>Devon</p>
</div>
<div id="search">
<p>search</p>
</div>
<div id="variety">
<p>variety</p>
</div>
So if Brighton is typed into the place input i need the form to submit the page and show the Brighton div and if Devon is typed in to show the Devon div etc and if the 2/12/2012 is typed into the date picker input and Brighton into the place input it goes to the page and shows the variety div.
i also need it so if the 1/12/2012 is typed in to the date picker input the page redirects to the page show.html.
any help would be greatly appreciated
thanks.
This is easy if you know PHP at all. It looks like you need a good, easy start. Then you will be able to achieve this in seconds.
Refer to W3SCHOOLS PHP Tutorial.
To achieve what you have mentioned, first make the following changes in your form:
<form action="submit.php" method="post">
<input id="place" name="place" type="text">
<input name="datepicker" type="text" id="datepicker">
<input type="submit" name="submit" id="submit" />
</form>
Create a new file called submit.php and add the following code:
<?php
$place = $_POST['place'];
$date = $_POST['datepicker'];
if ($date == '1/12/2012') {
header('Location: show.html');
exit;
}
?>
<?php if ($place == 'Brighton''): ?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php elseif ($place == 'Devon'): ?>
<div id="devon">
<p>Devon</p>
</div>
<?php elseif ($place == 'search'): ?>
<div id="search">
<p>search</p>
</div>
<?php elseif ($place == 'Variety'): ?>
<div id="variety">
<p>variety</p>
</div>
<?php endif; ?>
Now the above example is not the complete solution, but it gives you an idea as to how you can use if-then-else construct in PHP to compare values and do as desired.
Post your form to a php page and then check the posted form parameters to determine which div to show.
<?php
if ($_POST["place"] == "Brighton") {
?>
<div id="brighton">
<p>Brighton</p>
</div>
<?php
} else if ($_POST["place"] == "Devon") {
?>
<div id="devon">
<p>Devon</p>
</div>
<?php
}
?>
Do that for each div and parameter combination. Make sure you set the "method" attribute on your form to "post":
<form action="somepage.php" method="post">...</form>
In the resulting HTML you will only see the one that matches the form parameter.

Categories