I have a dialog form.
<div id="dialog-form" title="Create new Admin">
<p class="validateTips">All form fields are required.</p>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" class="text ui-widget-content ui-corner-all" />
<label for="role">Role</label>
<select name="user_role" class="select ui-widget-content ui-corner-all" >
<option value="administrator">Administrator</option>
<option value="visitor">Visitor</option>
<option value="Helper">Helper</option>
</select>
<label for="email">Email</label>
<input type="text" name="login_email_admin" id="login_email_admin" value="" class="text ui-widget-content ui-corner-all" />
<label for="Passoword">Password</label>
<input type="passowrd" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
<label for="Passoword">Re-Enter Password</label>
<input type="password" name="password_2" id="password_2" value="" class="text ui-widget-content ui-corner-all" />
<input style="margin-top:15px;" type="submit" name="add_admin" value="Add New Admin">
</fieldset>
</form>
i am using following php code in views to take data from this dialog form when it will be submitted..
<?php
if($_POST['add_admin'])
{
$this->user_role=$this->input->post('name');
$this->user_role=$this->input->post('user_role');
$this->login_email_admin=$this->input->post('login_email_admin');
$this->password=$this->input->post('password');
$this->load->database();
$this->db->insert('admin_user',$this);
}
?>
But it's not inserting into the db.this problem really stuck me.i am calling page itself when form submit, i dont know what is the reason its not working.
additionally is there a way that i will get rid of using models, instead i can do all db operations in views?
Add an echo to print the variable ;)
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
enctype="multipart/form-data">
This code is wrong in many aspects:
if($_POST['add_admin'])
{
$this->user_role=$this->input->post('name');
$this->user_role=$this->input->post('user_role');
$this->login_email_admin=$this->input->post('login_email_admin');
$this->password=$this->input->post('password');
$this->load->database();
$this->db->insert('admin_user',$this);
}
You're overwriting the user_role property;
you're passing the whole $this reference, which contains way much more than those properties
that's not how you do an insert with Active Record! Field names must be passad as an array, not as object properties
Should be something like:
$field['user'] = $this->input->post('name'); //or whatever is the FIELD NAME
$field['user_role'] = $this->input->post('user_role');
$field['login_email_admin'] = $this->input->post('login_email_admin');
$field['password'] = $this->input->post('password');
$this->load->database();
$this->db->insert('admin_user',$field);
See insert chapter on manual for reference.
Also, I don't know why you want to do that inside a view, you should do the insert in a model, and the checking for the form being submitted must be done in the controller (ideally you could use the Form Validation class, which is very handy for this task.
You're using a framework with an MVC architecture but in this piece of code you're taking advantage of almost nothing from both...
1) You need to pass the $_POST to your view, in the controller...
$this->load->view("MyView",array('_POST'=>$_POST));
2) You wont need PHP for your solution to post to self
<form action="" method="post" enctype="multipart/form-data">
Browser default when action is blank is self. An alternative (more absolute) solution could also be $_SERVER['REQUEST_URI']
If you wanted to be more CI, you could do this inline (with validation class instantiated and form helper included)
<?php echo form_open(''); ?>
Additionally $_SERVER['PHP_SELF'] wont work because you're running from index.php, your URL is controlled via .htaccess.
This question has been answered already, but I want to point out that form_open() without any arguments would do exactly what you want (creating an action="").
So you can simply use the snippet below:
<?php echo form_open(); ?>
Here is a reference from the CodeIgniter's Source:
function form_open($action = '', $attributes = '', $hidden = array())
Related
I am trying to save a Users data from an input field so that it can be displayed later in their profile of a webpage, for example the user inputs data of a cinema(name, address) and can see it later under Saved Restaurants and call up the previously saved information. Can the PHP and HTML code be written together in one .PHP file?
So far I have this:
<html lang>
<head>
<link rel="stylesheet" href="css/addOrEditCinemaPage.css">
</head>
<?php include "php/head.php" ?>
<?php include "php/navigation.php" ?>
<body>
<div class="myForm">
<form>
<h2>Add or Edit a Cinema</h2>
<label for="name"><b>Name of Cinema</b></label>
<input type="text" placeholder="Enter name">
<label for="str"><b>Street</b></label>
<input type="text" placeholder="Enter street">
<label for="nr"><b>Nr.</b></label>
<input type="number" placeholder="Enter Nr.">
<label for="plz"><b>Post Code</b></label>
<input type="number" placeholder="Enter Post Code"><br><br>
<label for="ct"><b>City</b></label>
<input type="text" placeholder="Enter City">
<label for="sta"><b>State</b></label>
<input type="text" placeholder="Enter State">
<label for="descr"><b>Description</b></label><br>
<textarea placeholder="Enter Discription"></textarea>
<div class="imagebutton">
Add Image
</div>
<button type="submit">Save</button>
</form>
</div>
<?php include "php/footer.php" ?>
</body>
</html>```
Can the PHP to save and display to input infomration also be written here?
Yes, you can by setting the 'action' attribute of the form to the same file, and by setting the 'method' attribute to POST.
Instead of using
<form>
use
<form action="<?PHP echo $_SERVER['php_self'];?>" method="POST">
Then, set the 'name' attribute of each input.
For example, Instead of using
<input type="text" placeholder="Enter name">
use
<input type="text" name="name" placeholder="Enter name">
You'll also have to set the 'name' attribute of the submit button to 'submit':
<button type="submit" name="submit">Save</button>
Once you've done that, the PHP code to access the form data would be:
if (isset($_POST['submit'])) {
echo $_POST['name'];
}
send your data with html to another php page
you must use get or post for sending form data to php page
like below
I have two different sites and want to pass data via a form from the first to the second page.
The form looks as follows (simplified):
<form method="POST" action="https://othersite.me/login.php">
<label class="title">
E-Mail
</label>
<input name="luser" type="email">
<label class="title">
Password
</label>
<input name="lpasswd" type="password">
<input type="submit" value="Login">
</form>
The php code othersite.me/login.php looks as follows:
<?php
$email=Format::input($_POST['luser']?:$_GET['e']);
$passwd=Format::input($_POST['lpasswd']?:$_GET['t']);
?>
<form method="POST" action="login.php">
<label class="title">
E-Mail
</label>
<input name="luser" type="email" value="<?php echo $email; ?>">
<label class="title">
Password
</label>
<input type="password" value="<?php echo $passwd; ?>">
<input type="submit" value="Submit">
</form>
Now I expect that when I enter some data on the first page, the data is transferred to otherpage.me/login.php and displayed in the appropriate fields.
Curiously, after pressing the submit button, the website is redirected to othersite.me/login.php for less than a second and then automatically to othersite.me/index.php.
If I use GET instead of POST, the form works as expected and stays on othersite.me/login.php.
How is this possible and how can I fix that. Do you have some ideas?
Why do you need two php file to do this? You can use action="<?php echo $_SERVER['PHP_SELF']; ?>" in your login.php and start processing the submitted data there. You can follow this instruction
I don't have reputation to comment yet so I will answer.
In the othersite.me/login.php if you have permission paste the code below
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
This will return all warnings and fatal errors
I have a custom form in my wordpress theme archive.php file but I can't get the post of that form. It's empty.
I have these:
<?php print_r($_POST); ?>
<div class="filtros">
<h3>Búsqueda de documentos</h3>
<form action="#" method="post">
<input type="text" id="name" name="f_name" placeholder="Buscar" value="<?php echo $_POST['f_name']; ?>" />
<div class="dates">
<input type="text" id="inicio" class="date" name="f_inicio" placeholder="Fecha de inicio" value="<?=$_POST['f_inicio']?>" /> /
<input type="text" id="final" class="date" name="f_final" placeholder="Fecha final" value="<?=$_POST['f_final']?>" />
</div>
<div class="submit"><input type="submit" value="Buscar" /></div>
</form>
</div>
Even if I pass the variables throw the URL I cant get that vars with $_GET.
¿Any idea?
While it's not advisable from a security point of view, you can change the method on your form from method="post" to method="get" to have variables posted as $_GET variables instead.
Change action="#" to action="<? echo $_SERVER['REQUEST_URI']?>"
Try change <?= to <? echo
You may need to point the action="myfile.php" to specific file.. See the solutions here:
$_POST returns empty on form submit in wordpress
I have a form in a Code Igniter view in my jQuery Mobile application.
<form action="<?= BASE_PAGE_URL ?>settings" method="post" id="settingsForm">
<div data-role="fieldcontain">
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" value="" placeholder="First Name" />
</div>
<div data-role="fieldcontain">
<label for="lastName">Last Name</label>
<input type="text" name="lastName" id="lastName" value="" placeholder="Last Name" />
</div>
<input type="hidden" name="purpose" value="register" />
<input type="submit" name="submit" value="Register" />
</form>
However, when I write this code into the controller method that the URL specified by action leads to:
echo ($_SERVER['REQUEST_METHOD'] == 'POST') ? "yay" : "nay";
"nay" is written to the page when I hit the submit button. How come Code Igniter cannot tell that I am submitting a post request?
If you want to determine if you have POST data or if request was a POST, use post() method from the input class
$this->input->post(index);
//returns FALSE if no POST data
//returns the POST array if there is data (hence, a POST)
//returns a specific data from the array if you provide "index"
It can be cause of register_globals. Use
if($_POST)
OR CI input class
$this->input->post('var');
I wonder if your form is returning a post request as you expect, but that something is redirecting immediately afterwards, which you're seeing as a GET? Try putting a temporary exit() immediately into the form-handling action; I suspect that will come out as POST.
I have a form with a submit button and a handler that stores data in the database. Problem is when the form is submitted, all data is cleared from the input fields. Is there a way to still show them after submit? What changes do I need to make to my form_submit function?
function mymodule_form_submit($form, &$form_state) {
//how to retain the input in the form
}
I'm looking for the most "drupalish" way to get this done?
As indicated by this previous StackOverflow question you can accomplish this with $form_state['storage'] and $form_state['rebuild'].
You can access the data using $_REQUEST['form_variable_name'] where form_variable_name is the name of the html input tag.
You then need to render the page back putting this value into the input tags value field.
<form method="POST" action="/account/contactdetails/">
<div>
<label>First name:</label>
<input type="text" name="firstname" value="<?php echo $_REQUEST['firstname']; ?>" />
</div>
<div>
<label>Last name:</label>
<input type="text" name="lastname" value="<?php echo $_REQUEST['lastname']; ?>" />
</div>
<input type="submit" value="Save" />
</form>