CodeIgniter refresh page after form submit - php

I am trying to refresh a page via CodeIgniter, but I can't seem to get this to work.
Basically, we have a list of jobs on a page and a user has the option to filter the said jobs.
So, a user will select checkboxes and then click save to save their selections to a database based on their user id.
I will obviously need the data to refresh once they have saved their selection, but I have tried many things and this doesn't seem to work at all. I assume I've got the redirect in the wrong place, which is currently in the controller.
I've tried the following:
$this->load->helper('url');
redirect('dashboard', 'refresh');
But nothing happens.
I have the above redirect in the controller function:
public function save_filters() {
if (isset($_POST)){
$filters = $_POST['client_ids'];
}
$this->load->model('dashboard_model');
$this->dashboard_model->update_dashboard_filters($filters);
$this->load->helper('url');
redirect('dashboard', 'refresh');
}
Did I put it in the wrong place?
I get no errors or warnings from firebug?
The form:
<form id="filters_form">
<table>
<tr>
<td>
<label for="GAB Robins">GAB Robins </label><input type="checkbox" name="client_ids[]" value="4">
</td>
<td>
<label for="Cunningham Lindsay">Cunningham Lindsay </label><input type="checkbox" name="client_ids[]" value="5">
</td>
<td>
<label for="Lloyds">Lloyds </label><input type="checkbox" name="client_ids[]" value="7">
</td>
<td>
<label for="Sundry/Private">Sundry/Private </label><input type="checkbox" name="client_ids[]" value="9">
</td>
<td>
<label for="WNS Ltd">WNS Ltd </label><input type="checkbox" name="client_ids[]" value="4441">
</td>
<td>
<label for="Stream">Stream </label><input type="checkbox" name="client_ids[]" value="4493">
</td>
<td>
<label for="Redesign">Redesign </label><input type="checkbox" name="client_ids[]" value="5295">
</td>
</tr>
</table>
<input type="button" value="Save selections" name="save_filters" id="save_filters_button" />
</form>
The javascript that deals with the form
save_filter: function () {
$.post('/dashboard/save_filters', $('#filters_form').serialize(), function (response) {
if (response.status == 'ok') {
if ($('.ui-success').length == 0) {
$('#page_main').before('<div class="ui-success" style="margin-bottom: 1em; font-weight: bold; border-bottom: 1px solid #ccc; padding: 1em;"><img src="/common/images/icons/accept.png" style="margin-right: 10px;" align="absmiddle" />Filters saved!</div>').fadeIn(1000);
}
}
else {
$(response.errors).each(function (i, item) {
alert(item);
});
}
});
}

Put the action in form
action="<?php base_url(); ?>controller_name/function_name" and method="post"
Example:
<form id="filters_form" action="<?php base_url(); ?>controller_name/function_name" method="post">
UPDATE: Because you are using JS, you are not refreshing page. Don't use JS if you want to refresh the page.

Its not working because you have an Ajax request, and you cant make redirect on server before you return data to JavaScript that made that request.
you can make redirect based on your response

Related

unable to store my all retrieve the data from the Mysql table to my new html form.

Here i am facing a problem with the while loop.. (i think) unable to store my all retrieve the data from Mysql table to my html form.
the first row is only getting posted and the rest of the rows are not posting due to same name element getting repeated from the while loop,
Here the $_POST['mechanic_name']; one time i'm using.. any problem with this..
because this is not in a while loop, ore if you think any other problems with the code below pls advice
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
</tr>
<!--* For save php script*-->
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,) VALUE ('$mechanic_ID','$mechanic_name'[enter image description here][1])";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
plz help me...!`
<div>
<form role="form" method="post" action="schedule.php">
<table class="table table table-inverse table-hover">
<fieldset>
<div>
<input class="form-control" placeholder="Username" name="month" type="Month" value="January">
</div>
<thead class="thead-inverse">
<tr>
<th>Mechanic Id</th>
<th>Mechanic Name</th>
<th>Woking Day Selection</th>
<th>Delete User</th>
</tr>
</thead>
<?php
include("db_conection.php");
$view_users_query="select * from mechanic";//select query for viewing users.
$run=mysqli_query($dbcon,$view_users_query);//here run the sql query.
while($row=mysqli_fetch_array($run))//while look to fetch the result and store in a array $row.
{
$mechanic_ID=$row[0];
$mechanic_name=$row[1];
?>
<tr>
<td>
<input name="mechanic_ID" type="text" value="<?php echo $mechanic_ID; ?>">
</td>
<td>
<input name="mechanic_name" type="text" value="<?php echo $mechanic_name; ?>">
</td>
<td>
<div class="weekDays-selector">
<input type="checkbox" name="Sun" id="weekday-sun" class="weekday" />
<label for="weekday-sun">S</label>
<input type="checkbox" name="Mon" id="weekday-mon" class="weekday" />
<label for="weekday-mon">M</label>
<input type="checkbox" name="Tue" id="weekday-tue" class="weekday" />
<label for="weekday-tue">T</label>
<input type="checkbox" name="Wed" id="weekday-wed" class="weekday" />
<label for="weekday-wed">W</label>
<input type="checkbox" name="Thu" id="weekday-thu" class="weekday" />
<label for="weekday-thu">T</label>
<input type="checkbox" name="Fri" id="weekday-fri" class="weekday" />
<label for="weekday-fri">F</label>
<input type="checkbox" name="Sat" id="weekday-sat" class="weekday" />
<label for="weekday-sat">S</label>
</div>
</td>
<td>
<!--button-->
<input class="btn btn-lg btn-success btn-block" type="submit" value="register" name="register" >
</td>
</tr>
</fieldset>
<?php } ?>
</table>
</form>
</div>
<?php
include("db_conection.php");//make connection here
if(isset($_POST['register']))
{
$mechanic_ID=$_POST['mechanic_ID'];//here getting result from the post array after submitting the form.
$mechanic_name=$_POST['mechanic_name'];//same
$month=$_POST['month'];//same
$Sun=$_POST['Sun'];//same
$Mon=$_POST['Mon'];//same
$Tue=$_POST['Tue'];//same
$Wed=$_POST['Wed'];//same
$Thu=$_POST['Thu'];//same
$Fri=$_POST['Fri'];//same
$Sat=$_POST['Sat'];//same
if($mechanic_name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
//insert the user into the database.
$insert_schedule="insert into schedule (mechanic_ID,mechanic_Name,month,Sun,Mon,Tue,Wed,Thu,Fri,Sat) VALUE ('$mechanic_ID','$mechanic_name','$month','$Sun','$Mon','$Tue','$Wed','$Thu','$Fri','$Sat')";
if(mysqli_query($dbcon,$insert_schedule))
{
echo"<script>window.open('index.html','_self')</script>";
}
}
?>
ok
your text input have same static name
you shuld give it an array name if you want to save it like :
<input type="text" name="mechanic_ID[]" value="first_one" />
<input type="text" name="mechanic_ID[]" value="second_one" />
and when get data
$mechanic_ids = $_POST['mechanic_ID'];
// this give you data in array ('first_one','second_one')
can resume
foreach($mechanic_ids as $mechanic_id){
// do some thing in this one
}
here good example about arrays form html and php
here is the things is mechanic table whole data (mechsanic_ID , Mechanic_name) able to retrieve, it's showing all the data; because of while loop, But when register it the very first row only able to posted and the rest of the rows are not postinginterface look like this.

Execute query on button click If checkbox is checked

I want to execute a script if checkbox is checked when I click on the submit button.
I got the following code snippet in my overview.php:
<table style="width: 100%; font-size: 11;">
<tr>
<td colspan="6">
<form method="post" action="update.php" target="_blank">
<script language="JavaScript">
function toggle(source) {
checkboxes = document.querySelectorAll("input[name^='box[']");
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = source.checked;
}
}
</script>
<input type="submit" name="update" value="Update - LoL / BoL">
<input type="checkbox" onClick="toggle(this)" name="All" value="All"> Select all
</form>
</td>
</tr>
<tr>
<th>ve067</th>
<th>ve378</th>
<th>ve724</th>
<th>ve725</th>
<th>ve726</th>
<th>ve727</th>
</tr>
<tr>
<td>
<form method="post" action="update.php">
<input type="checkbox" name="box[1]" value="ve067_LB1"> LeagueBot 1<br>
<input type="checkbox" name="box[2]" value="ve067_LB2"> LeagueBot 2<br>
<input type="checkbox" name="box[3]" value="ve067_LB3"> LeagueBot 3<br>
<input type="checkbox" name="box[4]" value="ve067_LB4"> LeagueBot 4<br>
</form>
</td>
</tr>
</table>
And this is a code snippet from my update.php:
<?php
if(isset($_POST['update'] && $_POST['box[4]']) {
// code here
}
?>
But it does not work for me.
You can do it as I have mentioned in a post on pastebin
Hope it helps you dude.
Solution: Consider the following JSFiddle
Explanation: This will validate to see that at least checkbox 'box[4]' has been ticked.
The "key" code here being the return function in the following line.
<input type="submit" name="update" value="Update - LoL / BoL" onclick="return validate();">
Update: Made to accommodate extra information given in comment.
Remember: The validation here is done on the client side so that it doesn't need to query the server (PHP) first

2 forms on same page php

My studybook gives me an assignment which requires me to have 2 forms on one page and output them both to the same page. Is this even possible? Both forms work fine independently. Both have the action:
<?php echo $_SERVER['PHP_SELF']; ?>".
Both have a submit button, but the button only submits the form it's part of. This probably makes sence though.
Thing is i need the page to either post both form outputs when clicking one of the 2 submit buttons or press them subsequently but the information from the first form needs to stay on the page.
Is this possible or am i trying do to the impossible?
the forms are as follows;
form 1:
<form name="orderform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Korting:
<tr>
<td>
<input type="checkbox" name="korting[]" value=15 /> Student 15% <br>
<input type="checkbox" name="korting[]" value=10 /> Senior 10% <br>
<input type="checkbox" name="korting[]" value=5 /> Klant 5% <br>
<hr />
</td>
</tr>
<tr>
<td>
betalingswijze
<input type="radio" name="betalingswijze" value="paypal"> Paypal
<input type="radio" name="betalingswijze" value="mastercard"> Mastercard
<input type="radio" name="betalingswijze" value="visa"> Visa
<hr />
</td>
<tr>
<td>
<img src="toshiba.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Toshiba Sattelite A100-510 Basisprijs 999.99
</td>
</tr>
<tr>
<td><!--Shopping Cart Begin-->
<input type="hidden" name="toshibaproduct" value="001" />
<input type="hidden" name="toshibamerk" value="toshiba" />
<input type="hidden" name="toshibamodel" value="Sattelite A100-510" />
Operating system <select name="toshibaos" value="Toshiba">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="toshibaaantal" value="0" />
<input type="hidden" name="toshibaprijs" value="999.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
<tr>
<td>
<img src="acer.jpg" alt=" " />
</td>
</tr>
<tr>
<td>
Acer Aspire 5735Z Basisprijs 529.99
</td>
</tr>
<tr>
<td>
<input type="hidden" name="acerproduct" value="002" />
<input type="hidden" name="acermerk" value="acer" />
<input type="hidden" name="acermodel" value="Aspire 5735Z" />
Operating system <select name="aceros" value="Acer">
<option value="xp">Windows XP</option>
<option value="vista">Windows Vista</option>
<option value="linux">Linux</option>
</select>
Aantal: <input type="text" size=2 maxlength=3 name="aceraantal" value="0" />
<input type="hidden" name="acerprijs" value="529.99" />
<input type="image" src="bestel.jpg" border=0 value="bestellen" />
<hr />
</td><!--Shopping Cart End-->
</tr>
</form>
Form 2
<form name="klant gegevens" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border=1 >
<tr>
<td colspan="2">
<b>Factuur klantgegevens</b>
</td>
</tr>
<tr>
<td width="100">Naam: </td>
<td>
<input type="text" sie="55" name="naam" />
</td>
</tr>
<tr>
<tr>
<td>Adres: </td>
<td>
<input type="text" sie="55" name="adres" />
</td>
</tr>
<tr>
<td>Woonplaats:</td>
<td>
<input type="text size="34" name="woonplaats">
Postcode:<input type="text" size="6" name="postcode">
</td>
</tr>
<tr>
<td>e-mail:</td>
<td>
<input type="text" size="55" name="email">
</td>
</tr>
<tr>
<td>Feedback:</td>
<td>
<textarea cols="40" rows="3" name="commentaar">
</textarea>
</td>
</tr>
</table>
<input type="image" src="checkout.png" value="send"/>
</form>
Both have functions which kick in on submit. Sorry for the spacings. I have them better in my own files but i just don't know how to get them right on this site.
Greetings,
Lennart
The action represent the page that will receive the posted data. You may use differents actions or the same action with different parameters.
If you use the same action, you had to insert a parameter that permit to manage different cases. You may insert an hidden field to do this.
Consider these simple forms:
<form name="form_a" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="A">
<button type="submit">Form A</button>
</form>
<form name="form_b" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="hidden" name="form" value="B">
<button type="submit">Form B</button>
</form>
To manage the different submission, you had to check the value of the hidden field:
if(isset($_POST['form'])){
switch ($_POST['form']) {
case "A":
echo "submitted A";
break;
case "B":
echo "submitted B";
break;
default:
echo "What are you doing?";
}
}
You can't submit two separate forms at the same time, because each submission represent a different request to the server.
You may merge manually the fields of the two forms, or use Javascript to do this for you.
Keep in mind that if you do this via Javascript, the field of the forms had to have differents names.
As you caan see here you can do simply via jQuery:
var str1 = $("form_a").serialize();
var str2 = $("form_b").serialize();
$.post(url, str1 + "&" + str2);
Where url is the action params of the form
Your form should be like this.
First form
<form method="post" >
...
<input type="hidden" name="form1submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
Second form
<form method="post" >
...
<input type="hidden" name="form2submission" value="yes" >
<input type="submit" name="submit" value="Submit" >
</form>
And your php for first form.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form1submission'])) {
// first form code.
}
And second form php code.
if('POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['form2submission'])) {
// second form code.
}
That's it.
DUPLICATE POST
Yes you can!
First, add the proper action to all forms, secondly, give them an unique name
HTML Side
Don't forget to add the http method what you want (GET or POST)
<form method="post">
<input type="hidden" name="orderform" />
<!-- rest of form goes here -->
</form>
<form method="post">
<input type="hidden" name="klant_gegevens" />
<!-- rest of form goes here -->
</form>
Leaving the action-attribute empty or removing it entirely will make the form submit to the current page (see this post), and usage of $_SERVER["PHP_SELF"] can lead to XSS-injection read under "Big Note on PHP Form Security"
Note:
Avoid using space for field names, it can make some problem to match them...
PHP Side
getting input values, by filtering on received form name
if (isset($_POST["orderform"])) {
// The first form was submitted
}
if (isset($_POST["klant_gegevens"])) {
// The second form was submitted
}
Note:
Use print_r() or var_dump(), to debug the content of exchanged values

How can I submit a form and use php to insert a new row into a mysql database table?

Below is my PHP code. I think I'm close but I don't understand why when I click the submit button on the form it isn't entering my if (isset($_POST['addFormula'])) statement in my PHP code to perform the insert.
alert('IM IN HERE!!!');
alert('successfully added ');
alert('error while adding formula...');
Here is my html code
<table style="width:100%">
<tr>
<td>
<form method="post">
<h3 style="color:#a5a5a5; margin:5px 0 0 0">Name:</h3>
<input type="text" name="name"><br>
<h3 style="color:#a5a5a5; margin:5px 0 0 0">Formula:</h3>
<input type="text" name="formula"><br>
<h3 style="color:#a5a5a5; margin:5px 0 0 0">Category:</h3>
<input class="radio-button" type="radio" id="math" name="category" value="Math" checked>
Math
<input class="radio-button" type="radio" name="category" value="Physics">
Physics
<h3 style="color:#a5a5a5; margin:5px 0 0 0">Description:</h3>
<textarea class="description-textarea" name="description" ></textarea><br>
<tr>
<td class="cancel-button">
<button type="button" id="cancelAdd">Cancel</button>
</td>
<td class="save-button">
<button type="submit" name="addFormula" id="add">Save</button>
</td>
</tr>
</form>
</td>
</tr>
</table>
try this, I added the action element to the form to load the same php page. You can change it to a specific page if the PHP script is not on the same file.
Example:
<form method="post" action='action.php'>
<form method="post" action=<?php echo $_SERVER['PHP_SELF'];?>>

Getting radio button value by ajax

I want to get radio button values and send them through AJAX to PHP.
My AJAX is running but is currently inserting a 0 in each row, so it's not picking up the value from the radio button. Any help would be appreciated.
$("#save_privacy").submit(function() {
var message_pri = $("#message_pri").val();
var follow_pri = $("#follow_pri").val();
var post_pri = $("#post_pri").val();
var check7 = $("#check7").val();
var s = {
"message_pri": message_pri,
"follow_pri": follow_pri,
"post_pri": post_pri,
"check": check7
}
$.ajax({
url: 'edit_check.php',
type: 'POST',
data: s,
beforeSend: function() {
$(".privacy_info").html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success: function(data) {
$(".privacy_info").html(data);
}
});
return false;
});
<form id="save_privacy">
<table align="center" width="70%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td style="padding: 5px;">
<b>Message Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="message_pri" name="message_pri" value="1" /> ON
<input type="radio" id="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" id="message_pri" name="message_pri" value="3" /> FOLLOWERS
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Follow Buttun: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="follow_pri" name="follow_pri" value="1" /> ON
<input type="radio" id="follow_pri" name="follow_pri" value="2" /> OFF
</td>
</tr>
<tr>
<td style="padding: 5px;">
<b>Who Can Post On Your Profile: </b>
</td>
<td style="padding: 5px;">
<input type="radio" id="post_pri" name="post_pri" value="1" /> Evry one
<input type="radio" id="post_pri" name="post_pri" value="2" /> Followers
</td>
</tr>
<tr>
<td colspan="2" style="padding: 5px;">
<input type="hidden" id="check7" value="save_privacy" name="check7" />
<input class="small color blue button" type="submit" value="Save" />
</td>
</tr>
</table>
<div class="privacy_info"></div>
</form>
Firstly you have a lot of duplicated id attributes, which is incorrect. Use classes instead, then use the :checked selector to get the specific instance of the radio which was selected.
Try this:
<input type="radio" class="message_pri" name="message_pri" value="1" /> ON
<input type="radio" class="message_pri" name="message_pri" value="2" /> OFF
<input type="radio" class="message_pri" name="message_pri" value="3" /> FOLLOWERS
var message_pri = $(".message_pri:checked").val();
And so on for your other radio inputs.
dont use id two time first thing
now for selected value of radio box use
$("input:radio[name=post_pri] :selected").val();
I would like to add that it is best to use the onChange event instead of the onClick event on the radio fieldset button call to the AJAX function. I am not sure why, but in this case it posts the correct value every time. When using the onClick event it sometimes posts a value different from the checked value. It is not much but it will definitely save somebody somewhere from a slight headache.
Example of the radion button group:
<fieldset **onChange="return rating_score()"** id="rating_selectors" data-
role="controlgroup" data-type="horizontal">
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"1"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_0"
value="1" />
<label title="1" for="ratings_0"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"2"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_1"
value="2" />
<label title="2" for="ratings_1"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_2"
value="3" />
<label title="3" for="ratings_2"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"4"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_3"
value="4" />
<label title="4" for="ratings_3"></label>
<input <?php if (!(strcmp($row_rs_new_rating['rating_value'],"5"))) {echo
"checked=\"checked\"";} ?> type="radio" name="rating" id="ratings_4"
value="5" />
<label title="5" for="ratings_4"></label>
</fieldset>
Example of the AJAX function:
<script type="text/javascript">
function rating_score ( txt_rating )
{ $.ajax( { type : "POST",
data: {"txt_captcha" : $("#txt_captcha").val(), "txt_rating" :
$("input[name=rating]:checked").val()},
url : "functions/reviewrater.php",
success : function (txt_rating)
{
$('#rating-container').load(document.URL + ' #rating-container');
$('span.stars').stars();
},
error : function ( xhr )
{ alert( "error" );
}
} );
return false;
}
</script>
Quick explanation:
The onChange event in the fieldset sends the value of the checked radio button to the AJAX function. I have added validation for the rest of the elements on the page, so the <?php if (!(strcmp($row_rs_new_rating['rating_value'],"3"))) {echo "checked=\"checked\"";} ?> compares the value stored in the rating session and retrieves the value again, so the user does not have to click on the rating again once they are warned that some of the other elements are empty. It looks much more complicated than it is, but all I actually wanted to say was to use the onChange instead of the onCLick event. :-)
You can visit this page to see the above code in action:
Rental Property Reviews Page
Try this use serialize function check serialize here
$("#save_privacy").submit(function(){
var serialise = $("#save_privacy").serialize();
$.ajax({
url:'edit_check.php',
type:'POST',
data:serialise,
beforeSend: function (){
$(".privacy_info") .html("<img src=\"style/img/ajax/load2.gif\" alt=\"Loading ....\" />");
},
success:function(data){
$(".privacy_info") .html(data);
}
});
return false;
});

Categories