-5

i have a simple form using codeigniter and there is a submit button:

<?php echo form_open('myController/add'); ?>
<input type="text" class="form-control" name="myInput" value='abc'>
<button type="submit" name="Submit">Save</button>
<?php echo form_close(); ?>

but it doesn't contain Submit when i run this code:

function add(){
  print_r($_POST);
}

here is the result:

Array ( [myInput] => abc )

i think the result should be like this:

Array ( [myInput] => abc [Submit] =>)

please help.. thank you.

Phil
  • 157,677
  • 23
  • 242
  • 245
oka
  • 1
  • 3
  • How are you submitting the form? By hitting the "Save" button or hitting the `Enter` key while typing in the text field? – Phil Nov 16 '17 at 05:05
  • Is there any JavaScript that intercepts the form submission? – Phil Nov 16 '17 at 05:06
  • @shyammakwana.me that is not good advice – Phil Nov 16 '17 at 05:06
  • @Phil I know but I can't see any other error or typos. so just a try. – shyammakwana.me Nov 16 '17 at 05:07
  • Demo showing that it *should* work just fine here ~ https://jsfiddle.net/2gdfuzd4/. I suspect there's some JavaScript messing with the form submission – Phil Nov 16 '17 at 05:14
  • Here's an example showing how a JavaScript form submission will omit the button name / value pair ~ https://jsfiddle.net/2gdfuzd4/2/ – Phil Nov 16 '17 at 05:21
  • @shyammakwana.me i tried both button and input but nothing. – oka Nov 16 '17 at 05:26
  • @Phil i dont have any javascript in this form. yeah, it work just fine in jsfiddle and still i dont know ahy it doesnt work on my project – oka Nov 16 '17 at 05:28
  • @oka can you be certain there's no JS involved? Try disabling JavaScript in your browser (just google how to if you don't know) and try again – Phil Nov 16 '17 at 05:29
  • @Phil thankyou, it works when i disabled javascript. so how prevent this issue while i need to enable javascript – oka Nov 16 '17 at 05:38
  • @oka find the JavaScript that's intercepting your form submission and remove / alter it. This might help ~ https://stackoverflow.com/questions/2066162/how-can-i-get-the-button-that-caused-the-submit-from-the-form-submit-event – Phil Nov 16 '17 at 05:39
  • @phil i actually getting confused, i think i dont have any javascript form submission, i mean, yes right there is a javascript but i dont use $("form").submit(function() { } ); or something like document.forms["myform"].submit(); – oka Nov 16 '17 at 06:00
  • It's impossible to help you any further. Check for **any** JS you're including on that page. Could be a 3rd party form validation script or something like that – Phil Nov 16 '17 at 06:04
  • @Phil here is my own javascript https://pastebin.com/gbTSReNy , and the other javascript is from bootstrap, jquey, etc – oka Nov 16 '17 at 06:14
  • It's the `etc` I'd worry about – Phil Nov 16 '17 at 06:15
  • what about this? custom.min.js:1 Uncaught TypeError: this.submit is not a function at HTMLFormElement. (custom.min.js:1) at HTMLFormElement.dispatch (jquery.min.js:3) at HTMLFormElement.r.handle (jquery.min.js:3) – oka Nov 16 '17 at 06:38

3 Answers3

0

Per Mozilla's documentation for the button element, you can have a value attribute. See here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

This means you could just do this:

<button type="submit" name="Submit" value="something">Save</button>

and then you would have:

$_POST['Submit'] = "something";

BUT in your question it looks like you want the Submit key to be present, even if there is no value, and I don't think that's going to work. I do believe you will have to have something as a value in order for the key to be present.

Brian Gottier
  • 4,522
  • 3
  • 21
  • 37
  • 1
    *"I don't think that's going to work"* <- that's where you're wrong. A *named* button of type *submit* will send whatever its `value` property is, even if the attribute is not set in which case it will send an empty string – Phil Nov 16 '17 at 05:18
  • Well, I don't know if it's appropriate to change my answer, and you are probably right (or you wouldn't have said I was wrong). I think I was thinking about the behavior of jQuery ajax posts where the value is empty. – Brian Gottier Nov 16 '17 at 05:24
0

When you start the project with codeigniter framework then you need to insert data into database. I think you need to tyr this code for get data 'name' => $this->input->post('name').

For example:

class Controller extends CI_Controller {
function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->database();
        $this->load->model('Mymodel');
}
public function user_registration(){
$data = array('name' => $this->input->post('name'),'email' => $this->input->post('email'),'password' => $this->input->post('pwd'));
$this->Mymodel->UserRegistration($data);
}
}
Flexo
  • 87,323
  • 22
  • 191
  • 272
-1

try this

use

<input type="submit" name="Submit" value="Save">

instead of

<button type="submit" name="Submit">Save</button>

because $_post get value of input

but you can also get its value using its name

like

$formSubmit = $this->input->post('Submit');
echo $formSubmit;
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39