Unable to reset form in codeigniter - php

I'm using three different methods to reset my form, but still nothing happens after I click the buttons, Here is my form's structure:
Any help would be appreciated
$this->load->helper('form');
$attributes = array('class' => 'form-inline', 'id' => 'ProductForm');
echo form_open_multipart("product/process/$id",$attributes);
.
.
.
<?php echo form_reset(array('id'=>'reset','value'=>'resetme'));?>
<button onclick="ResetForm();">Reset Form</button>
<button type="reset" value="Reset">Reset</button>
<?php echo form_close();?>
<script>
function ResetForm() {
document.getElementById("ProductForm").reset() ;
}
</script>
//same for form_open()

Did you also try
<input type="reset" value="Reset">

#Mahdi Younesi: Just use the input type reset and then test it will reset the form elements.

first add some input types in form to test the reset function
here is the solution
add one input in form and than
replace form_reset(array('id'=>'reset')); to form_reset(array('id'=>'reset','value'=>"resetme"));

Related

Button Form POST Not Working

I have a simple button that hides once the event is triggered
<?php
//Define attributes
echo'<input type="submit" id="toggler" name="add_friend"class=button
value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
</input>';
?>
//Hide the button
<script>
var hidden = false;
function action() {
if(!hidden) {
document.getElementById('toggler').style.visibility = 'hidden';
}
}
The above works as it should no problems , However when I add form to get method=POST for the button does not hide nor does my POST make it to $_POST['add_friend']
echo ' <form method="post" >
<input type="submit" id=toggler name="add_friend" class="button"
value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
</input>
</form>';
How can I make correct this so that the button hides and my POST is passed on to my isset code please .
if (isset ($_POST['add_friend'])){
//rest of my code once the button is clicked and hidden
Thanks in advance .
Your JS is most likely hiding the element. Then your form gets submitted (the POST), only for the page to refresh and the button reappear.
It seems to me that you want to hijack form submission and process the request with ajax.
The following example code shows a similar problem with the Php form processing. You could adapt to your liking (I have left out the required Javascript):
<?php
$feedback = null;
$people = array(
1 => 'Samuel',
2 => 'Fred',
3 => 'Roger',
4 => 'Mavis'
);
$friends = array(3); // i.e. Roger
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$add_friend = isset($_POST['add_friend']) ? $_POST['add_friend'] : null;
if(array_key_exists($add_friend, $people) && !in_array($add_friend, $friends)) {
array_push($friends, $add_friend); // You probably want to save state here
$feedback = 'Added ' . $people[$add_friend] . ' as friend.';
}
}
?>
<?php echo isset($feedback) ? $feedback : ''; ?>
<form method="post">
<?php foreach ($people as $key => $person) { ?>
<button name=
"add_friend" onClick=
"action();" value=
"<?php echo $key ?>"
<?php echo in_array($key, $friends) ? 'disabled' : '' ?>
>
Friend <?php echo $person ?>
</button>
<?php } ?>
</form>
Checkboxes may be a better fit than buttons here.
A couple things wrong with this. You have an extra double quote before 'method' in your form, and should also add action="#" to the <form> tag. This tells the browser to send the result of the form to the current page. It's also good practise to add a hidden field to send your data, rather than adding it to the submit button. Try this and see if it works.
if (isset($_POST['add_friend'])) {
var_dump($_POST['add_friend']);
}
echo '
<form method="post" action="#">
<input type="hidden" name="add_friend" value="'.$output1['username'].'">
<input type="submit" id="toggler" class="button" value="Add '. $output1['username'].' As A Friend ?" onClick="action();"/>
</form>
';
Bear in mind this will essentially reload the page, so if you want to make an asynchronous request (EG, send some request without loading the page again) you will need to look into a solution with AJAX.
unless you didn't supply more information or copy/paste everything, you seem to have extra quotations here:
echo ' <form "method="post" >
Wrapping the input in a form will natively submit the form as well as fire your javascript. If you want to use an ajax solution, tie into the submit event and prevent the default action. (using jquery here):
$( "form" ).on( "submit", function( event ) {
event.preventDefault();
// serialize() will get the form data, which can be used in ajax call
console.log( $( this ).serialize() );
});

two submit button inside one form in codeigniter

hi i want to have two submit button in one form in code igniter. is it possible? i want to make two buttons which will do the same thing, add data into the database, the only difference of the buttons is on which page they will redirect.
here is my codes,
<?php
echo form_open('EmpFamilyInfo/add_childinfo/'.$this->uri->segment(3));
?>
// some textboxex,
<div class="box-body">
<div class = 'col-md-6 col-sm-offset-6'>
<button class="btn btn-info fa fa-save" type="submit">&nbsp Save</button>
<a href = '<?php echo base_url().'EmpFamilyInfo/other_childinfo/'.$this->uri->segment(3); ?>' class = 'btn btn-primary fa fa-save' >&nbsp Add Another</a>
<a href = '<?php echo base_url().'EmpFamilyInfo/parentsinfo_father/'.$this->uri->segment(3); ?>' class = 'btn btn-danger fa fa-arrow-circle-right'>&nbsp Skip</a>
<?php
echo form_close();
?>
</div>
</div>
i have made this code which the first link echo form_open('EmpFamilyInfo/add_childinfo/'.$this->uri->segment(3)); what i wanted this to do is
public function other_childinfo(){
$this->form_validation->set_rules('NAME', 'Name of Child' ,'trim|required|max_length[100]');
if($this->form_validation->run($this) == FALSE){
$this->child_info();
}else{
if($query = $this->EmpFamilyInfo_Model->insert_childinfo()){
redirect('EmpFamilyInfo/child_info/'.$this->uri->segment(3));
}else{
$this->child_info();
}
}
}
but the error is,that it does not have post data. how can i make this link a submit button but it will go to different function or how can i make it have the post data?
this question ahs been answered already on this site, so this response is not mine, just quoting for you:
This uses javascript so keep in mind users who have this disabled will not be able to use this solution.
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('form1').action = action;
document.getElementById('form1').submit();
}
</script>
...
<input type="button" onclick="submitForm('page1.php')" value="submit 1" />
<input type="button" onclick="submitForm('page2.php')" value="submit 2" />

How to make a button not to submit the form?

<form>
<button type=submit>save</button>
<button onclick="create();return false;">click</button>
<textarea id="some">Testing</textarea>
</form>
<script>
function create(){
window.location.href="some.php";
}
</script>
Though I have added return false in the onclick event, when I click the button it gets submitted. How to make that button to stop from form submission. If I have some other function in the create() function instead of redirecting, return false code will stop from submission. But here am redirecting the page so return false is not working.
I tried putting return false code in create() function too but no luck.
How to stop the form submit ?
<button type=submit>save</button>
Should be:
<input type=submit value="save" onclick="create();return false;"/>
And
<button>click</button>
USE..
<input type="button" onclick="create();">
As in create function you have redirected page so actually form is not submited but page is redirected.
If you use the button like
<button type="button">foobar</button>
it won't submit the form as long as you don't bind some js functions on it.
If you don't give this attribute type="button" the form takes it as a normal button and trys to submit it. Same like if you would add the attribute type="submit".

included php code is not executing on submit

I'm including a a php code with angular ui routing
the problem is if the millitary.php :
`
<div class="jumbotron">
<div class="page-header">
<h1>Millitary Loot Table</h1>
</div>
<?php
$MillitaryLoot = array(
'M4A4' => 47,
'AWP' => 2,
'Karambit' => 1,
'Famas' => 50
);
$newMillitaryLoot = array();
foreach ($MillitaryLoot as $item=>$value)
{
$newMillitaryLoot = array_merge($newMillitaryLoot, array_fill(0, $value, $item));
}
$myLoot = $newMillitaryLoot[array_rand($newMillitaryLoot)];
if (isset($_POST['submit']))
{
echo "\n" . "<h2 style='display:inline;'>Item: </h2>" . '<p class="text-center text-danger">' . $myLoot . '</p>' . "</br>";
}
?>
<form name="Gamble" action="" method="post" target="_self">
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
</br>
</div>
`
the include app.js
$stateProvider
.state('millitary', {
url: '/millitary',
templateUrl: 'inc/millitary.php'
})
is included into the index.php the submit button basicly not executes the code above the form
so basically i want the php code to execute in the index.php to keep the ui instead of jumping into the executing php file and losing the index.php linked styles and navigation
edit : if i run the code normaly it works but it doesn't if it gets included
edit2 : everything outside the if statement works basically if i add a echo "test"; it outputs but inside the if it does not so the problem is the form and the connection to the if statement
kind regards, daniel
Change your HTML to the following:
<form id="form" name="Gamble" method="post">
<button id="form" name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
You can submit your form with a button with HTML5 but it must share the same ID of the form. Also keep in mind this will only be supported with browsers that support HTML5 so if not they won't be able to submit the form.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
The other alternative is to use...
<input name="submit" type="submit" class="btn btn-primary" value="Gamble">
as #gbestard suggested.
Change
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
to
<input name="submit" type="submit" class="btn btn-primary" value="Gamble" />
In order to submit you need a type submit input or use some client side script as javascript

Change a form in order to use jQuery and avoid the page refresh

I'm trying to change the form tag below in order to use jQuery. Already, clicking the buttons changes the display from rows to columns and vice-versa but I want to avoid the page refresh. I'm really new at jQuery and can't honestly say what my mistakes are when trying to change it myself.
<form id="rowsToColumns" action="index.php?main_page=specials&disp_order=1" method="POST">
<input type="hidden" name="style_changer" value="columns"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Column</button>
</form>
<form id="columnsToRows" action="index.php?main_page=specials&disp_order=1" method="POST">
<input type="hidden" name="style_changer" value="rows"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Rows</button>
</form>
I'm also trying for the buttons to call a different stylesheet upon click. This stylesheet is not needed for the display to change from/to rows/columns as I mentioned above. The actual page is written using php as shown below:
<?php $this_page = zen_href_link($_GET['main_page'], zen_get_all_get_params()); ?>
<div id="style_changer">
<?php if($current_listing_style == 'rows') {?>
<form id="rowsToColumns" action="<?php echo $this_page;?>" method="POST">
<input type="hidden" name="style_changer" value="columns"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Column</button>
</form>
<?php } else { ?>
<form id="columnsToRows" action="<?php echo $this_page;?>" method="POST">
<input type="hidden" name="style_changer" value="rows"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Rows</button>
</form>
<?php } ?>
</div>
If the question is "how to change a form in order to use jQuery and avoid the page refresh", then the jquery form plugin is your friend, as it turns any html form into an ajax-powered one.
Simply follow their instructions and you'll get it working in no time (provided your form already works as is).
You can prevent the Default form Submission by preventing the default action on the submit button..
$('button[type=submit]').submit( function(e){
e.preventDefault(); // Stops the form from submitting
});
Well, for a very vague method you can use $.ajax and take advantage of reading the <form>'s pre-existing attributes to decide on submission method and read the elements' values as submissiong data:
$('form').on('submit',function(e){
var $form = $(this);
// submit the form, but use the AJAX equiv. instead of a full page refresh
$.ajax({
'url' : $form.attr('action'),
'method' : $form.attr('type'),
'data' : $form.serialize(),
'success' : function(response){
// process response (make CSS changes or whatever it is
// a form submission would normally do)
}
});
// prevent the normal submit and reload behavior as AJAX is now
// handling the submission
e.preventDefault();
});
However, for this to work you'll need some variation of a stripped-down PHP response just for the purpose of the AJAX request (avoid resending headers, script tags, etc. and just return the raw data that jQuery can use to make a UI decision).

Categories