naeplus
naeplus

Reputation: 121

Codeigniter $_SERVER['PHP_SELF'] not working in views

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?

Upvotes: 0

Views: 5670

Answers (4)

Sina
Sina

Reputation: 607

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())

Upvotes: 0

Damien Pirsy
Damien Pirsy

Reputation: 25435

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);
    }
  1. You're overwriting the user_role property;
  2. you're passing the whole $this reference, which contains way much more than those properties
  3. 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...

Upvotes: 2

Matt Lo
Matt Lo

Reputation: 5731

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.

Upvotes: 1

rauschen
rauschen

Reputation: 3996

Add an echo to print the variable ;)

 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
       enctype="multipart/form-data">

Upvotes: 4

Related Questions