multiple JS form submit on change inside a PHP while - javascript

I have a while in PHP that is building a table. Inside my while, I have a dropdown menu that I want to execute code on change.... The form submit on change doesn'T work...
This is my code:
<?
while ($data2 = mysql_fetch_array( $data))
{ ?>
<form name="FormSize" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<td>
<select name="size[]" class="form-field3" OnChange="document.FormSize.submit();" >
<? foreach ($sizearray as $value) { ?>
<option value="$value" <? if($data2['size'] == $value) { echo "selected";} ?> ><? echo $value; ?></option>
<? } ?>
</select>
<input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">
</td>
</form>
<? } ?>
and on top of my page, I have this to execute the code..
<?
//submit size form
if (isset($_POST['size']))
{
foreach($_POST['product_id'] as $key => $id)
{
$product_id = $id;
$newsize = $_POST['size'][$key];
$sql3 = mysql_query("update cart SET size = '".$newsize."' where product_id = '".$product_id."' ");
}
?>
any idea why it's not executing ?

You have multiple forms with the same name="FormSize". So document.FormSize will be an array-like object, and you need to access the appropriate one. You can do this by using this.form to refer to the <form> that contains the <select>.
You also need to fix the element nesting -- the <form> has to be inside the <td>, not the other way around.
<?
while ($data2 = mysql_fetch_array( $data))
{ ?>
<td>
<form name="FormSize" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" >
<select name="size[]" class="form-field3" OnChange="this.form.submit();" >
<? foreach ($sizearray as $value) { ?>
<option value="$value" <? if($data2['size'] == $value) { echo "selected";} ?> ><? echo $value; ?></option>
<? } ?>
</select>
<input type="hidden" name="product_id[]" value="<? echo $data2['product_id']; ?>">
</form>
/td>
<? } ?>

Could be because the html is not valid, you cannot put your form tag as a direct child of a tr.
Apart from that, naming all your forms the same will make it pretty hard to submit a specific one by its name.
As you are using arrays for your input names, I assume that you mean all of them to be in the same form, so you should just have your form wrap the table and take it out of the while loop.

Related

Getting the value of a select, with onchange and add row id number

Hi guys I'm having a hard time with this one please help me.
So I have an add row function where I can add dynamic rows to the form. My plan is to get the value of the Particulars with onchange event. So when you change the particular if will get the current value of the selected Particulars specifically on the selected row only.
The form looks like this:
So to test this out I'm trying to put the value in alert messages.
So this is the one I've done so far, I don't know if I'm making this correctly.
This is my html code.
<table class="table table-bordered" id="addSubPaymentTable">
<thead>
<tr>
<th >Particulars</th>
<th style="width:10%;"><button type="button" class="btn btn-success btn-sm" onclick="addPaymentRow()">Add Row</button></th>
</tr>
</thead>
<tbody>
<?php $arrayNumber = 0; for($x = 1; $x < 2; $x++) { ?>
<tr id="row<?php echo $x; ?>" class="<?php echo $arrayNumber; ?>">
<td class="form-group">
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance('<?php echo $x; ?>')" /> //AS YOU CAN SEE I HAVE AN ONCHANGE EVENT HERE
<option value="">Select Particulars</option>
<?php foreach ($particularsData as $particulars)
{
?>
<option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>
<?php
}
?>
</select>
<?php $arrayNumber++; } // /.foreach?>
</tbody>
So this is the function for the onchange event of Specific Balance
Javascript
// Get Specific Balance
function specificBalance(row = null)
{
var particular = $(this).val();
alert('Testing value is' +particular );
}
Please help me to get the value of the Particular, that is different in every row.
replace this line
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance('')" />
with
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance(this)" />
and in the jquery function use this
function specificBalance(row = null)
{
var particular = $(row).val();
alert('Testing value is' +particular );
}
Use this.value in onchange() function for select tag in HTML
<select class="form-control particulars" name="subparticulars[<?php echo $x; ?>]" id="subparticulars<?php echo $x; ?>"
onchange="specificBalance(this.value)" /> //AS YOU CAN SEE I HAVE AN ONCHANGE EVENT HERE
<option value="">Select Particulars</option>
<?php foreach ($particularsData as $particulars)
{
?>
<option value="<?php echo $particulars['feetype_id']; ?>"> <?php echo $particulars['feetype_name']; ?> </option>
<?php
}
?>
</select>

Codeigniter update select options based on other select option

So, I am fairly new to codeignitor and was hoping to get some help. Below I have my controller(Estimates.php), my model(Invoice_items_model.php) and my view(_add_edit_items.php). These are shortened versions as the full versions are pretty long.
What I am looking to do is have it so that when one of the select options is clicked it will then check the $suboption['non_compatible'] field for that suboption. If the suboption in any of the other select options matches that non_compatible field it will be disabled.
I found this post that I could probably work with but i'm not sure how to do an onchange event for dynamically generated selects.
Estimates.php
`public function estimate($id = ''){$data['platforms'] = $this->invoice_items_model->get_platforms();
$data['options'] = $this->invoice_items_model->get_options();
$data['suboptions'] = $this->invoice_items_model->get_suboptions();
$this->load->view('admin/estimates/_add_edit_items', $data);}`
Invoice_items_model.php
public function get_platforms()
{
$platforms = array();
$this->db->order_by('name', 'asc');
return $this->db->get('tblplatforms')->result_array();
}
public function get_options()
{
$options = array();
return $this->db->get('tblplatform_options')->result_array();
}
public function get_suboptions()
{
$suboptions = array();
return $this->db->get('tblplatform_suboptions')->result_array();
}
}'
_add_edit_items.php
<?php foreach($options as $option) { ?>
<tr>
<td></td>
<td><p><?php echo $option['name']; ;?>:</p></td>
<td>
<select name="<?php echo $option['name']; ?>" id="<?php echo $option['name']; ?>" rows="4" class="form-control">
<?php foreach($suboptions as $suboption) { ?>
<?php if($suboption['plat_option'] == $option['name']) { ?>
<option value="<?php echo $suboption['name']; ?>" id="<?php echo $suboption['name']; ?>"><?php echo $suboption['name']; ?></option>
<?php } ?>
<?php } ?>
</select>
</td>
</tr>
<?php } ?>
<tr>
keep your code as it is just change the event like this.
$(document).on("onChange", "your_selector", function() {
// your code
}

How to continue two type of loop at a time using Javascript and PHP

I need one help.i need to continue 2 types of loop at a time using Javascript and PHP.Let me to explain my code and scenario as well.
<script>
function addQuestionField(){
var get =$("#ques").val();
if(get==null || get==''){
alert('Please add no of questions');
}else{
var counter = 0;
if (counter > 0){
return;
}else{
counter++;
<?php
$status=array("status"=>'1');
$feeddata=$db->kf_answertype->find($ustatus);
?>
for(var i=1;i<get;i++){
$('#container').append(' <textarea class="form-control" name="questions'+ i +'" id="questions" placeholder="Questions" style="background:#FFFFFF;" rows="2"><?php if($_REQUEST['edit']) { echo $getcustomerobj->questions; } else { echo $_REQUEST['questions']; } ?></textarea>');
<?php
foreach($feeddata as $v){
?>
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>');
<?php
}
?>
}
}
}
}
</script>
Here i have one for loop but inside this one foreach loop is still there .By doing this in one iteration of for loop foreach loop continuing many times which i dont need.Here i need each time of for loop the foreach loop also iterate.suppose for 1st iteration of for loop the foreach loop also iterate once and so on.Please help me.
Use current and next functions to get consistent elements of an array. So, this fragment of your code
foreach($feeddata as $v){
?>
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>');
<?php
}
?>
should be changed to
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo current($feeddata)['_id']; ?>"> <?php echo current($feeddata)['answertype']; ?>');
<?php next($feeddata); ?>
or, if you use old version of php do it in such way:
<?php $v = current($feeddata); ?>
$('#subcontainer').append(' <input type="radio" name="answer_type0" id="answer_type0" value="<?php echo $v['_id']; ?>"> <?php echo $v['answertype']; ?>');
<?php next($feeddata); ?>

Can only post first result of while loop

I am using a while loop to display results from a query. The while loop is working fine. In hidden fields I would like to post the values of userID and accessID to the user details page. I am submitting the form using javascript to submit from a link. My problem is that regardless of the username I click I can only post the values for the first displayed record. What am I doing wrong?
The code:
<?php
while($row = $result->fetch_array()) { ?>
<form method="post" action="edit_user.php" id="userForm">
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
</form>
<?php } ?>
The javascript used for submitting the form:
function submitForm() {
var form = document.getElementById("userForm");
form.submit();
}
Thank you.
EDIT - I don't want to pass the values in the url.
you are generating multiple <form>s inside loop, move your <form> outside while loop, like:
<form method="post" action="edit_user.php" id="userForm">
<?php
while($row = $result->fetch_array()) { ?>
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID[]" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID[]" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
<?php } ?>
Submit
</form>
You're running into trouble because of this line
var form = document.getElementById("userForm");
In Javascript and HTML, an ID is supposed to be unique to a certain DOM element. In this case, you've got a whole load of form tags that have the same ID. You need to give each form a different ID, and then pass that ID to the submitForm function.
For example:
<?php
$id = 0;
while($row = $result->fetch_array()) { ?>
$id++;
<form method="post" action="edit_user.php" id="<?php echo "userForm".$id ?>">
<tr>
<td>
<?php echo $row['firstname'].' '.$row['surname']; ?>
<input type="hidden" name="userID" value="<?php echo $row['userID']; ?>" />
<input type="hidden" name="accessID" value="<?php echo $row['accessID']; ?>" />
</td>
</tr>
</form>
<?php } ?>
and then
function submitForm(id) {
var form = document.getElementById(id);
form.submit();
}
edit: how do I php? :D

How to dynamically create buttons names and detect which button is pressed in PHP?

This is an experiment int PHP that I'm currently working on. It'll be part of a bigger file.
The idea is, the program will generate a set of submit buttons in a form and a unique name will be assigned to each button in this convention: btn[the order of button]
The page will then poll to find if any button was pressed using isset(). Here's what I've got so far.
<html>
<head>
<title>Test Dynamic Naming</title>
</head>
<body>
<form action="testDynamicNaming.php" method="POST">
<?php
$row = 9;
for($i = 0; $i < $row; $i++) {
$name = 'btn['.$i.']';
?>
<input type="submit" name="<?php echo $name; ?>" value="<?php echo $name; ?>"/>
<?php
echo "<br>";
}
?>
</form>
<?php
extract($_REQUEST);
for($i = 0; $i < $row; $i++) {
$name = 'btn['.$i.']';
if(isset($_POST[$name])) {
echo "<br><br>".$name.' is pressed.';
}
// OR
if(isset($$name)) {
echo "<br><br>".$name.' is pressed.';
}
}
?>
</body>
</html>
I managed to set the name and values of the button as I wanted but the page couldn't detect the button that was pressed. What am I missing here, or is there any other way that I could accomplish the same task?
if(isset($_POST['btn'])){
foreach($_POST['btn'] as $key => $value){
echo "Button {$key}:{$value} pressed";
}
}
you may use
if(isset($_POST['btn'][$i])) {
echo "<br><br>".$_POST['btn'][$i].' is pressed.';
}
Maybe use javascript?
easy e.g.
<input type="submit" name="<?php echo $name; ?>" onclick="document.getElementById('<?php echo $name; ?>_pressed').value='true';" value="1"/>
<input type="hidden" id="<?php echo $name; ?>_pressed" name="x1_pressed" value="false">
if(isset($_POST[$name]) == "true") {

Categories