On my page I have multiple forms all the same as this...
<form class='bill-upd'>
<input type='hidden' value='".$info['rand']."' name='rand2' id='rand2'>
<input type='hidden' value='".$info['id']."' name='billid' id='billid'>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' onClick='updateBill();' />
</form>
I then have my AJAX as so
function updateBill()
{
$.post('update_bill.php', $('.bill-upd').serialize(),
function(data) {
$(this).append(data);
});
};
If I have one form on my page, this works fine, but when there are multiple instances, my same record is being overwritten, can anybody tell me where im going wrong?
Thanks
With the help of #Armatus and #Bricriu I got it sorted, the answer as marked worked, stupidly forgot to wrap it within document.ready
You need a way to specify which bill you're updating, otherwise it just takes all of them. This should work, since we can determine which one it is by which button was clicked.
Edit: I personally would bind the even using jQuery rather than onClick on the element itself:
$(".bill-upd input:submit").click(function(){
var elem = $(this);
$.post('update_bill.php', elem.parent('.bill-upd').serialize(),
function(data) {
elem.append(data);
});
});
If the other forms have the same class .bill-upd the javascript .serlialize() will take values from them also. Make sure the other forms have an alternate class.
What would be better would be if you gave the form a unique id such as id="billform" and then replace $('.bill-upd').serialize() with $('#billform').serialize().
Hope this helps.
EDIT:
Use this:
$(this).parent('.bill-upd').serialize()
to serialize the parent of that which was clicked.
Related
I have a form with a get method that is used for searching items (vehicles) from the database. The form has multiple checkbox sets. Like user can filter the search results by selecting multiple makes from the makes checkbox list. Similarly user can also apply filter on car models from models chebox list and so on. There are about 10 such filters on the form.
Here is the code snippet of the my form:
<form action='./search' method='get'>
<input type='checkbox' name='make[]' value='BMW'/>
<input type='checkbox' name='make[]' value='Mercedes'/>
<input type='checkbox' name='make[]' value='Honda'/>
<input type='checkbox' name='make[]' value='Toyota'/>
<input type='checkbox' name='make[]' value='Porsche'/>
......//Remaining Form
</form>
Similarly for car models I have similar markup...There are such 10 filters all implemented using checkbox list.
Now coming towards the problem, when I submit the form I get URL like this:
http://localhost/auto/search?make=BMW&make=Mercedes&make=Honda
This is forming a type of query string which I don't like i.e it is repeating 'make' attribute for all the checked values and will do so for remaining 9 filters as well. This will result in very long ugly looking URL.
What I want is that my URL should look something like this:
http://localhost/auto/search?make=BMW,Mercedes,Honda
This is much better but I don't know how would I achieve that. What I have tried is to get all the values of checked boxes and then write them into hidden field value so that I get my desired format in the query string. And unset all the checboxes selected by the user and submit the form with hidden field containing all the selections. But the problem is when the form is submitted I get two 'make' fields in the URL like:
http://localhost/auto/search?make=&car_makes=BMW,Mercedes,Honda
where car_makes is the hidden input field in which I wrote all the selections in value attribute.
Any solution to this? So that make attribute does not get submitted, but it does, since it is the part of the form.
Thanks.
Try these:
<html>
<head>
<script type="text/javascript">
function buildup(){
var makes=document.getElementsByName('make[]');
var m=document.getElementById('make');
m.value='';
ms='';
for (var i = makes.length - 1; i >= 0; i--) {
if(i>0)ms=ms+',';
ms=ms+makes[i].value;
}
m.value=ms;
document.getElementById('form').submit();
}
</script>
</head>
<body>
<input type='checkbox' name='make[]' value='BMW'/>
<input type='checkbox' name='make[]' value='Mercedes'/>
<input type='checkbox' name='make[]' value='Honda'/>
<input type='checkbox' name='make[]' value='Toyota'/>
<input type='checkbox' name='make[]' value='Porsche'/>
<form action='./search' id='form' method='get'>
<input type='hidden' name='make' id='make'>
<input type='button' value='submit' onclick='buildup()'>
</form>
</body>
If you don't want the make inputs to get submitted, you should disable them before the form gets submitted (or remove the name attribute...).
It would probably be easiest to add a class to all inputs you don't want to submit and then do something like this right before the form submit:
html:
...
<input type='checkbox' name='make[]' value='BMW' class='dont-send-class' />
...
js:
$('.dont-send-class').prop("disabled", true);
// now you can submit the form
Maybe this is a stupid question, but I was wondering if I could get information passed with a form into a for-loop.
The problem is as follows:
The passing of the information works, though he passes only for the last loop. For example if I click the submit button for loop 2 ($i = 2). The command $_POST['titel'] will only remember the information in last loop ($i = $numact-1) and not loop 2 ($i = 2).
for example if titel[0] = test0, titel[1] = test1, titel[2] = test2. and I click the submit button below titel[0] he passes the information from titel[2]. Is there an easy way to get around this?
I have the following code (For the sake of simplicity I shortened it);
<?php
for ($i = 0; $i <= $numact-1; $i++) {
echo "<tr><td width='150'>
<input type='text' name='titel' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>
You're using the same parameter name over and over again (name=titel - you had a type and probably meant to write as 'title') so when the form submits - only one value will be passed.
You can easily fix that by passing the parameters as:
...
echo "<tr><td width='150'>
<input type='text' name='titel$i' value='$titel[$i]' />
</td></tr>
<tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
...
and reading it on the other side as title0, title1 etc.
As seen in this post, you can pass in an array simply by putting brackets around the index. PHP will reconstruct the array for you.
Also, you probably want your submit at the end of your table, not after each row. Your code should look something like this:
<?php
for ($i = 0; $i <= $numact-1; $i++) {
echo "<tr><td width='150'>
<input type='text' name='titel['.$i.']' value='$titel[$i]' />
</td></tr><tr><td><input type='submit' name='submitreg' value='Toon activiteit'/>
</td></tr>";
}
?>
The advantage of this approach is that it's more extensible, should you choose to use different values. However, Edson Junior's approach is slightly simpler.
You can change the name attribute to name=titel[] so PHP will automatically transform it to an array. It is not necessary to specify the index inside title[].
For example:
<input type="text" name="titel[]" value="x" />
<input type="text" name="titel[]" value="y" />
<input type="text" name="titel[]" value="z" />
When you submit the form, PHP will transform it to
"title" = array (
0 => "x",
1 => "y",
2 => "z"
);
In case you need to check what button was clicked, you could change the inputs type="submit" to <input type="button" name="submitreg" class="submitreg" id="whatever_you_want_but_has_to_be_unique" />.
Outside the loop, add another input wich will have the value of what button was pressed.
<input type="hidden" id="buttonPressed" name="buttonPressed" value="" />
Then add this code (you need to import jQuery library to the page so it understands the code below, wich is jQuery, here is the Docs.
<script type="text/javascript">
$(function(){
$("button.submitreg").live("click", function(){
var buttonPressed = $(this).attr("id");
("input#buttonPressed").val(buttonPressed);
});
}
</script>
All you need to do is get the buttonPressed value and you'll know what button was pressed.
I have the following form:
<form name='progObj_form' method='POST' enctype='multipart/form-data' action='processpage.php'>
<select name='manageObj[]' id='objectives' multiple="multiple">
<option value=0>there are no objectives for this program</option>
</select><br />
<a href='#nogo' onclick="delItem(objectives,0,'objEditBtn')" class='shiftOpt'>delete selected</a><br />
<input name='newObjective' type='text' id='newObjective'/>
<input name='addNew' type='button' onclick="AddItem(newObjective.value,6,'objectives','objEditBtn');" value='add objective'/>
<input name="passProgID" type="hidden" value="1" /><br />
<input name="objectiveEdit" id="objEditBtn" type="submit" value="save changes" disabled=disabled/>
</form>
that allows data (objectives in this case) to be added and deleted from a list box. That all works well but for some reason the updated listbox values aren't being passed to the process page.
I'm catching the data like so (simplified):
if (isset($_POST['objectiveEdit'])){
$progID=$_POST['passProgID'];
for ($v=1;$v<count($_POST['manageObj']);$v++){
$value=$_POST['manageObj'][$v];
$sqlObj="INSERT INTO progObjective (progID,objective,objectiveOrder) VALUES ($progID,$value,$v)";
$result = mssql_query($sqlObj,$linkProbation) or die('Query failed: '.$sqlObj);
}//end for ($a=0;$a<count($_POST['manageObj']);$a++)
$objMsg=print_r($_POST['manageObj']).$sqlObj;
}//end if (isset($_POST['objectiveEdit'])
For $objMsg, I get a response of 1 and the array doesn't print because ostensibly, it's empty which means that it also doesn't enter the for loop.
I can include the javascript as well but started with just this for simplicity since I'm probably just overlooking something obvious?!
Option elements are never sent to the server. Only the value of the selected option will be sent.
In your case, something like manageObj=x will be sent to the server, where x is the value of the option element that is selected by the user. It is possible that you misunderstand the [] when it's used in a name attribute.
You should try to find a different method if you want to send the objectives created by the user to the server. You could store the data in hidden inputs for example.
So I finally figured it out! There's no array because there are no selections made! I updated the submit button to call a SELECT ALL function and now it's all good.
<input name="objectiveEdit" id="objEditBtn" type="submit" value="save changes" onclick="selectAll('objectives',true)" />
name='manageObj[]' - shouldn't that be name='manageObj' ?
Basically what I'm trying to do is to DYNAMICALLY APPEND elements in a form and save their values to MySQL using PHP so far I've started adding but I don't know how to post their values.. Here is what I've done so far:
<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js" ></script>
<script>
$('document').ready(function(){
$('#save').live('click',function(e){
/*
this should be the function that gets and post all elements in php
*/
//maybe the use of $.post or .load() or ajax
});
$('#add').click(function(){
$("<br><input type='text' class='do'name='do'>").appendTo('form');
});
});
</script></head>
<body>
<form> //this was the form that im appending
<input type='text' class='do' name="do"><br>
<input type='text' class='do' name="do"><br>
<input type='text' class='do' name="do"><br>
</form>
<input type='button' id='add' value='[+]'>
<input type='button' id='save' value='go2'><br>
<div id='res'></div>
</body>
</html>
and i want to save all values in database using php so just for example here is the php
<?php
//
mysql_query("INSERT <blablabla HERE>");
?>
please help me with this stuff... thanks..
I think a simple snippet of code can answer this for you:
HTML:
<form method="post">
<input type="submit" id="btn_submit">
</form>
JQuery:
$("#btn_submit").mousedown(function() {
$("form").append("<input name='foo' value='bar'>");
});
PHP:
$myValue = $_POST["foo"];
mysql_query("insert into myTable (foo), ($myValue)");
On every add click, you need to make an ajax call to a page, where (the ajax-called page) the mysql-insert would be performed.
Consider using Jquery.ajax
Instead of trying to append the form while saving the information to a database, why not just pull the list from the database (Holding all significant values), then reload using AJAX after the 'appending', which will programmatically be just adding the value to the end of the database. Voila! Problem solved.
Please refer the jquery form plugin .http://jquery.malsup.com/form/
Here form submitted using ajax , so it will not refresh the form and also you will get all the values posted as what php does
change name of the form text boxes 'do' to do[]
$('#save').live('click',function(e){
/*
this should be the function that gets and post all elements in php
*/
//maybe the use of $.post or .load() or ajax
});
$('#add').click(function(){
$("<br><input type='text' class='do' name='do[]'>").appendTo('form');
});
<form> //this was the form that im appending
**<input type='text' class='do' name="do[]"><br>
<input type='text' class='do' name="do[]"><br>
<input type='text' class='do' name="do[]"><br>**
</form>
<input type='button' id='add' value='[+]'>
<input type='button' id='save' value='go2'><br>
I am building a step by step form. At the first step, people enter their email. Then after they clicked next step, it gets the security question from database.
Here is the problem, is it possible to get data from database without submitting the from? I found people used AJAX. I am new to it. like i have the first step code here:
<div>
step 1
<input type='text' name='email' id='email' maxlength="50" />
nextstepbutton goes here
</div>
and this is step 2 code:
<div>
step 2
<label><?php echo getSecurityQuestion($emailvalue) ?></label>
</div>
How can I pass the value of 'email' into $emailvalue?
give your next button an id. Then say if you're using jquery you could do something along the lines of
$('#next-button').click(function () {
$.get('filename.php', { action: 'get_security_question', email: $('#email').val() }, function (data) {
$('#security_question').val(data['message']);
});
});
over in your php something like
if ($_GET['action'] === 'get_security_question') return "Here's your question for {$_GET['email']}.";
That is very vague and just a general outline but should give you an idea of how it kinda works.
Well you can do it without a form but you would have to use AJAX eg:
<input type="text" name="email" id="email" maxLength="50" />
<input type="button" value="Send" onClick="submitEmail()" />
You would need a JS function like:
function submitEmail(){
var emailAdd = document.getElementByID("email").value ;
// here you would hook into whatever ajax httprequest method you prefer
}