Submit form does not work correctly - php

<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

Related

How to know if 'input submit button' is clicked (wordpress)?

I need to know if 'input submit button' is clicked.
I tried following php codes, and clicked a save_progress button, but it echos 'EMPTY'.
Would you please let me know how to know it the button is clicked or get the value of the button?
Existing code (submit page):
<form class="acfef-form -submit" action="" method="post" autocomplete="disableacf" novalidate="novalidate" data-widget="fc8926d" id="acf-form-fc7226d18999" enctype="multipart/form-data">
<div class="1"></div>
<div class="2"></div>
<div class="3">
<div class="3-1"><input class="acfef-submit-button" type="submit" value="Save" data-state="publish"></div>
</div>
<div class="4"><input class="save-progress-button" name="save_progress" type="submit" data-state="revision" value="Revision"></div>
</form>
php codes I tried in the submit page:
if(isset($_POST['save_progress'])) {
echo 'NOT EMPTY ';
}else{
echo 'EMPTY ';
}
if (empty($_POST['save_progress'])){
if( array_key_exists( 'save_progress', $_POST ) ){
Thank you.
Specify method="post" like
<form class="acfef-form" method="post">
Also you should place your PHP code into a WP Hook, like init.

How to change form from get to post?

the links of this form have question marks:
This is a file: sidebar.php
echo
<form method='get'>
<div class='left'>
<div class='btn-group-vertical'>
<!-- Vertically Stck button group -->
<button 'submit' name='Service)name' class='btn btn-default'>Service Name</button>
<button type='submit' name='YYY_Service' class='btn btn-default'>YYY Service</button>
</div>
</form>;
?>
This is a part of index.php:
ini_set('session.save_path','/path/to/session');
session_start();
error_reporting(E_ALL); ini_set('display_errors', 1);
$current_menu='empty.php'; //If no menu/button is selected get right column empty(empty.php)
if(isset($_REQUEST['Service'])){$current_menu='serviceform.php';}
if(isset($_REQUEST['YYY_Service'])){$current_menu='yyyform.php';}
This is showing URLS such as:
www.foo.com/index.php?Service=
www.foo.com/index.php?YYY_Service=
How should this form changed in order to show instead urls this way?
www.foo.com/serviceform.php
www.foo.com/yyyform.php
And this code in another file:
function Button_set($name, $newline)
{
if($newline){
?>
<br><br><input type="button" name="<?=$name?>" value="<?=$name?>" /input>
<?php
}
else if (!$newline){
?>
<input type="button" name="<?=$name?>" value="<?=$name?>" /input>
<?php
}
}
If I only change the form method to POST it doesn't work...
Change:
<form method="get">
to:
<form method="post">
Also add attribute "action" to site you want:
<form method="post" action="serviceform.php">
<form method="post" action="yyyform.php">
W3C link on form

Printing html page and doing some action using 1 button

I have a Print button which I want to do 2 actions when clicked:
Updating my database.
Printing the HTML page.
This is what I've done so far, but it's not working:
<form action="" method="POST">
<body >
<?php
$n=$_POST['ID'];
$a=implode("</br>",$n);
list($add, $ward) = explode("(!#!)", $a);
?>
<div id="container">
<p id="address">
<?php echo"$address";?>
</p>
<p id="ward">
<?php echo"$ward";?>
</p>
</div>
<input type="submit" name="Print" value="Print" />
<?php
if(isset($_POST['Print']))
{
?><script>javascript:window.print()</script><?php
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
}?>
<div id="footer">
</div>
</form>
After using this print button , my database is getting updated, but the print window is showing error(i.e the variables posted from other page are showing errors).
Can anyone please help me print and update at same time with this Print button?
If your page does not have it, add form tags. They are mandatory unless you want to AJAX the data. Also remove the semi-colon from the end of the sql
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php
if(isset($_POST['Print'])) {
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?><script>window.print();</script>
<?php } ?>
UPDATE: Perhaps you mean this, but I do not want to keep correcting HTML.
<?php
$n=$_POST["ID"];
$a=implode("</br>",$n);
list($add,$ward)=explode("(!#!)", $a);
?>
<body>
<div id="headerbg">
<div id="header-e1"><a align="left" href="escalationReport.php">Back </a>
</div>
<div id="header-e3"><a align="right" href="logout.php">Logout </a>
</div>
<h1><p>Issue Notice</h1>
</div>
<div id="container">
<p id="address">
<?php echo "$address";?>
</p>
<p id="ward">
<?php echo "$ward";?>
</p>
</div>
<form action="" method="POST">
<input type="submit" name="Print" value="Print" />
</form>
<?php if(isset($_POST[ 'Print'])) {
mysql_query( "UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
?>
<script>
window.print();
</script>
<?php } ?>
<div id="footer"></div>
</body>
you have an error in the update query, you have placed semicolon inside the query so please remove that so the query will be like this.
Make sure your form method is POST
mysql_query("UPDATE `source_main` SET `source_status`=3 WHERE `source_id`=1");
Make sure you are using input type button inside the form tag. And use post method for form.

insert form to database

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.

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