Welcome Guest, Not a member yet? Register   Sign In
Cancel button on a form
#1

[eluser]Unknown[/eluser]
Hi,

Following the example in the documentation and the video tutorial, it seems neat to be able to run validation rules, etc, on forms, and submit with something simple like:

Code:
if ($this->form_validation->run() == FALSE)
{
    $this->load->view('myform');
}
else
{
    $this->db->insert('mytable', $_POST);
    $this->load->view('formsuccess');
}

This is great for forms with a single, unnamed submit button. But if I add a cancel button to my form, I need to name the buttons in order to tell which is pressed:
Code:
<input type="submit" name="submitOK"      value="Submit Comment" />
<input type="submit" name="submitCancel"  value="Cancel" />

in the form, and then:

Code:
if ( array_key_exists('submitOK', $_POST) ) {...}

in the controller.

The problem is then that the POST array contains an array_key 'submitOK', which unlike all other POST entries does not correspond to an a database field for my INSERT action. The two options I can think of are:

1) Remove the array key with unset($_POST['submitOK']) - but doesn't seem right messing with the system's POST array
2) Creating a new array duplicating all elements except this one

Both options seem a bit messy, given the otherwise very clean form process provide by CI. Is there a better alternative?

Thanks,

Dan
#2

[eluser]umefarooq[/eluser]
you can use link for cancel process just put the page name you want to show if clicked

Code:
<a href="page.php">Cancel</a>
#3

[eluser]CroNiX[/eluser]
I use this:
Code:
if ($this->form_validation->run() === FALSE && $this->input->post('submitOK'))
{  //form was submitted and passed validation
}
#4

[eluser]saidai jagan[/eluser]
I use this

&lt;input type="submit" name="submitOK" value="Submit Comment" /&gt;
&lt;input type="button" name="submitCancel" value="Cancel" onclick="redirect('some controller');"&gt;
#5

[eluser]Tandubhai[/eluser]
&lt;input type=“submit” name=“submitOK” value=“Submit Comment” /&gt;
&lt;input type=“button” name=“submitCancel” value=“Cancel” onclick=“redirect(‘some controller’);” &gt;

Quote:&lt;script&gt;
function redirect(url){
location.href = url;
}
&lt;/script&gt;
#6

[eluser]andrewtheandroid[/eluser]
Alternatively you can make a link to where you want to redirect then style it as a button using CSS.
or use the <button> tag in a link.

Code:
// button link
<a href="index.php"><button>Cancel</button></a>

// span as a button then use css to style button using the same class as your other form buttons.
<a href="index.php"><span class="myButtonClass">Cancel</span></a>

// style
&lt;style&gt;
.myButtonClass { display:inline-block;width: 100px; height: 50px; border: 1px solid #000 }
&lt;/style&gt;
#7

[eluser]Aken[/eluser]
For usability purposes, a "Cancel" feature relating to a form should be a link. Definitely not a submit input or a button, and DEFINITELY not one right next to the submit button.
#9

[eluser]Colin Williams[/eluser]
Your model should remove the value before it performs the insert.

Also, why is everybody harking on him about the cancel button?
#10

[eluser]JoostV[/eluser]
Sorry, no offence meant.




Theme © iAndrew 2016 - Forum software by © MyBB