I created a list of radio groups and attached them to a product category. So what am doing is checking the groups that the category is meant to have then am going to then pick up the radio values in those groups available to the category and display them to the user to fill.
The next thing is I want to pass them to my ajax function which will then inject it into database. The problem is I can't get the unique name of the radio input in my ajax since I am loading it from the database.
<?php $notarray = DataDB::getInstance()->select_from_where('pro_print_group','product_category_id',$cat);
while ($row = mysqli_fetch_assoc($notarray)) {?>
<div class="col-sm-12">
<label for="quantity"><?php echo DataDB::getInstance()->get_name_from_id('group_n','print_type_group','print_type_group_id',$row['print_type_group_id']); ?></label>
<div class="printd-group">
<?php
$not = DataDB::getInstance()->select_from_where('print_type','print_type_group_id',$row['print_type_group_id']);
foreach($not as $ow):?>
<label class="printd">
<input name="prind<?php echo $row['print_type_group_id'] ;?>[]" id="prind<?php echo $row['print_type_group_id'] ;?>[]" value="<?php echo $ow['print_type_id'] ;?>" checked="" type="radio" class="name" data-price="0"> <?php echo $ow['name'] ;?>
</label>
<?php endforeach; ?>
</div>
</div>
<?php } ?>
Ajax Function
when form is submitted i call this function
function testpr() {
document.getElementById("load").style.display = "block";
var track = 'YU6758990';
var printd = $('#printd').val();
$.ajax({
type: 'post',
url: 'frverify.php',
data: "track=" + track + "&printd=" + printd + "&ins=testp",
success: function(data)
{
document.getElementById("load").style.display = "none";
jQuery('#get_prresult').html(data).show();
scroll_to('#messs');
}
});
}
PHP function
The ajax function then passes it to this php function where I am to receive the post request which in return inserts the selected radio values to the database and then returns the response back to my front end
function test(){
$track = $_POST['tack'];
$printd = $_POST['printd'];
echo DataDB::getInstance()->testp($track,$printd);
}
My guess is that there are more then one radiobutton generated, if that's the case you could try something like this.
<input name="printd[<?php echo $row['print_type_group_id'] ;?>]" id="<?php echo $row['print_type_group_id'] ;?>" value="<?php echo $ow['print_type_id'] ;?>" checked="" type="radio" class="name" data-price="0"> <?php echo $ow['name'] ;?>
Then in your Javascript change the way you retrieve the values of your form by using serialize.
data: "track=" + track + "&" + $('form#id.selector').serialize() + "&ins=testp"
Then inside your PHP script iterate trough the values with some sort of foreach loop
function test(){
$track = $_POST['tack'];
foreach($_POST['printd'] as $key => $value){
//$key => print_type_group_id
//$value => print_type_id
}
echo DataDB::getInstance()->testp($track,$printd);
}
Not sure what you want to do with the data and what it is you actually need but this should send you in the right direction.
Related
Sorry for disturbing again with my very basic question. First of all, sorry if my English is a little bit hard to understand. My current situation is I want to do a popup modal in my drag and drop boxes. In my popup modal, I can view and edit the details of the user based on what we click in the button in the box. The problem is, I cannot SELECT the data by id. But, when I SELECT all the data, the data appear in the modal boxes. But, it appears all the data. I just want the selected id. Back to my question for past few days, I've redo again to get more understanding on this popup modal part. I've done ajax and a little bit JavaScript, also, I tried to debug my code just what I've been told but I got an error saying "Parameter is missing" . What is causing by that ? I've done some reading about parameter but I still don't get the actual understanding about it. Can someone give an idea what is actually parameter is missing . And what I suppose to do by it?
Here what I've tried so far.
This is the button
<button data-id="<?php echo $row['userid'];?>" data-target="doubleClick-1" class='jobinfo' type='button' id='btnInfo' ondblclick="document.getElementById('doubleClick-1').style.display='block'">Info</button>
This is the modal popup
<div id="doubleClick-1" class="modal">
<label class="tabHeading">User Info</label>
<div class="contentTechJobInfo">
<div class="tech-details">
<div class="techClose" onclick="document.getElementById('doubleClick-1').style.display='none'" >×</div>
</div>
</div>
</div>
</div>
<script type='text/javascript'>
$(document).ready(function() {
$('.jobinfo').click(function() {
var userid = $(this).data('userid');
// AJAX request
$.ajax({
url: 'ajaxhome.php',
type: 'post',
data: {userid: userid},
success: function(response) {
// Add response in Modal body
$('.tech-details').html(response);
// Display Modal
$('#doubleClick-1').modal('show');
}
});
});
});
</script>
This my ajaxhome.php
<?php
$connection = mysqli_connect("", "", "");
$db = mysqli_select_db($connection, '');
if (!isset($_GET['userid'])) {
die("Parameter is missing!");
}
$userid = intval($_GET['userid']);
$query = "SELECT * FROM user WHERE userid ='$userid'";
$query_run = mysqli_query($connection, $query);
if ($query_run) {
while ($row = mysqli_fetch_array($query_run)) {
?>
<div class="input-box">
<label for="">Name</label>
<input type="text" id="username" name="username" value="<?php echo $row['username']?>">
</div>
<div class="input-box">
<label for="">Number</label>
<input type="text" id="usernumber" name="usernumber" value="<?php echo $row['usernumber']?>">
</div>
<div class="input-box">
<label for="">Class</label>
<input type="text" id="userclass" name="userclass" value="<?php echo $row['userclass']?>">
</div>
<button type="submit" id="submit" name="update" class="btn btn-primary"> Update Data </button>
<?php
if (isset($_POST['update'])) {
$username = $_POST['username'];
$usernumber = $_POST['usernumber'];
$userclass = $_POST['userclass'];
$query = "UPDATE user SET username='$username', usernumber='$usernumber', userclass='$userclass' WHERE userid='$userid'";
$query_run = mysqli_query($connection, $query);
if ($query_run) {
echo '<script> alert("Data Updated"); </script>';
header("location:homepage.php");
} else {
echo '<script> alert("Data Not Updated"); </script>';
}
}
} ?>
<?php
}
?>
In short, I think the problem comes from these lines of code in your modal:
var userid = $(this).data('userid');
you should replace it with
var userid = $(this).data('id'); // you should pass 'id' to .data() function instead of 'userid'
With your current code userid variable in your modal will always be undefined. It means it wont exist in $_GET when you send ajax request to PHP. And it causes your ajaxhome.php moves to die("Parameter is missing!");.
To get data-xxx attribute with jQuery, you should use pass 'xxx' to .data() function.
var xxx = $(this).data('xxx');
In your button, you are storing userid in data-id attribute
<button data-id="<?php echo $row['userid'];?>"
so if you need to get that userid you should pass 'id' into .data() function
Update:
In your ajax, you are using type: 'post', so in your php code you should check $_POST instead of $_GET
I don't think the value of user has been obtained
var userid = $(this).data('userid');
you can try
var userid = $(this).data('id');
The result of dropdownlist is did not match what I wanted the data that has been selected. I would like to know how to pass the jquery or javascript variable to php or that is another to way to match the dropdownlist data?
function editbtn(id){
$('#employee_id_edit').val(id);
var invoice_id = $('#user-user-list').find("#row_"+id).find("td:eq(1)").text();
var payment_id = $('#user-user-list').find("#row_"+id).find("td:eq(2)").text();
var category = $('#user-user-list').find("#row_"+id).find("td:eq(3)").text();
var sale_person = $('#user-user-list').find("#row_"+id).find("td:eq(4)").text();
var amount = $('#user-user-list').find("#row_"+id).find("td:eq(5)").text();
$('#invoice_id_edit').val(invoice_id);
$('#payment_id_edit').val(payment_id);
$('#product_type_edit').val(category);
$('#employee_edit').val(sale_person);
$('#amount_edit').val(amount);
}
<label for="employee">Sale Person</label>
<select id="employee_edit" name="employee_edit" class="form-control" autocomplete="off">
<?php
$employee_model = $registry->get('loader')->model('user');
$employee = $employee_model->getEmployee();
if(null !== $employee) {
foreach ($employee as $employee) {
?>
<option value="<?php echo $employee['username'];?>" selected><?php echo $employee['username']; ?></option>
<?php
}
} else {
?>
<option>No Employee Available</option>
<?php
}
?>
</select>
I think you should use jquery plugins like 'jquery select2'(https://select2.org/) or create some custom ajax request to send data from js to PHP backend and then get and handle a response from js part.
I'm trying to dynamically generate radio buttons with data in front of them. The data that is to be displayed in front of the radio button is based on a drop down selection, which also displays some data in a text box using javascript.
I tried taking the selected option in a string and use it in the next query, but I know I am doing it wrong.
Database Connection
$db = pg_connect("");
$query = "select account_name,account_code,address1,address2,address3 FROM
customers";
$result = pg_query($db,$query);
//NEW QUERY
$sql1= "select name from conferences";
$result1= pg_query($db, $sql1);
//END
//New Code
<select class="form-control" id="conference" name="conference">
<option value="">Select Conference...</option>
<?php while($rows1 = pg_fetch_assoc($result1)) { ?>
<option value="<?= $rows1['code']; ?>"><?= $rows1['name']; ?></option>
<?php } ?>
</select>
<br>
// END OF NEW CODE
Dropdown to select the data.
<select onchange="ChooseContact(this)" class="form-control"
id="account_name" name="account_name" >
<?php
while($rows= pg_fetch_assoc($result)){
echo '<option value=" '.$rows['address1'].' '.$rows['address2'].'
'.$rows['address3'].''.$rows['account_code'].'">'.$rows['account_name'].'
'.$_POST[$rows['account_code']].'
</option>';
}?>
</select>
Displaying data in the text area based on the selcted value using javascript. (The code works fine till here)
<textarea readonly class="form-control" style="background-color: #F5F5F5;"
id="comment" rows="5" style="width:700px;"value=""placeholder="Address...">
</textarea>
<script>
function ChooseContact(data) {
document.getElementById ("comment").value = data.value;
}
</script>
Displaying data in front of the radio buttons based on the selected option(This code works if I use some random value in the query, but not if I use the selected value 'account_code' from the previous query. I'm using POST GET method to carry the selected value)
<?php
//NEW CODE
$sql = "select order_number, order_date from orders where
customer_account_code = '3000614' and conference_code='DS19-'"; <-Data
gets displayed when put random value like this.
$code = $_GET[$rows['account_code']];
$conf = $_GET[$rows1['conference_code']];
$sql = "select order_number, order_date from orders where
customer_account_code = '$code' and conference_code= '$conf']"; <- But I
want to display the data against the selected value, i.e, the 'account_code'
in the variable $code from the dropdown select
//END
$res = pg_query($db,$sql);
while($value = pg_fetch_assoc($res) ){
echo "<input type='radio' name='answer'
value='".$value['order_number']." ".$value['order_date']."'>"
.$value['order_number'].$value['order_date']." </input><br />";
}
?>
I need to help to find a way to put the selected 'account_code' in a variable and use it in the $sql query.
Please try with this code : (It's work for me)
1- Add this line to your HTML <head>:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script>
2- Edit your CODE to this:
Dropdown to select the data:
<select class="form-control" id="account_name" name="account_name">
<option value=""></option>
<?php while($rows = pg_fetch_assoc($result)) { ?>
<option value="<?= $rows['address1'].' '.$rows['address2'].' '.$rows['address3'].'-'.$rows['account_code']; ?>"><?= $rows['account_name']; ?></option>
<? } ?>
</select>
Displaying data in the text area based on the selected value using jQuery:
<textarea readonly class="form-control" style="background-color: #F5F5F5;"
id="comment" rows="5" style="width:700px;" value="" placeholder="Address..."></textarea>
jQuery Code:
<script type="text/javascript">
$('#comment').val($('#account_name').val()); // MAKE A DEFAULT VALUE
(function($) {
$('#account_name').change(function() {
$('#results').html(''); // REMOVE THE OLD RESULTS
var option = $(this).val();
$('#comment').val(option);
// EDIT RADIO WITH AJAX
$.ajax({
type: "POST",
url: "path/test.php",
dataType:'JSON',
data: $('#account_name').serialize()
}).done(function(data) {
for (var i = 0; i < data.length; i++) {
// ADD RADIO TO DIV RESULTS
$('#results').append('<input type="radio" name="answer" value="'+data[i].order_number+'">'+data[i].order_date+'</input><br>');
}
});
});
})(jQuery);
</script>
after that, add this HTML to your page, to show RESULTS FROM AJAX DATA
<!-- RADIOs -->
<div id="results"></div>
3- Create a new file like path/test.php
in this file, use this CODE to return values with JSON :)
<?php
header('Content-type: application/json');
// CONNECT (JUST USE YOUR CUSTOM CONNECTION METHOD & REQUIRE CONFIG FILE IF YOU WANT)
$db = pg_connect("");
$value = explode('-', $_POST['account_name']);
// EXPLODE AND GET LAST NUMBER AFTER < - >
$code = (int) end($value);
$sql = "select order_number, order_date from orders where customer_account_code = '$code'";
$res = pg_query($db, $sql);
// CREATE JSON RESULTS
$is = '';
while($data = pg_fetch_assoc($res)) {
$is .= json_encode($data).', ';
}
// AND GET ALL
echo '['.substr($is, 0, -2).']';
?>
I have the following code which prints the values in a datatable.
<?php
foreach ($result as $val) {
?>
<input class="example" type="checkbox" value="yes" id="example" name="example"><?php echo $val->id; ?>
<?php } ?>
Now I want to send the value to the server when the checkbox is checked. I have the following code for the same, however I'm confused on how I can get the ID of the row which is echoed by PHP.
$(".example").change(function() {
var value = $('.example').attr('value');
console.log(value);
});
Thanks in advance.
You can set an another attribute to the checkbox like :
<input type="checkbox" data-id="<?php echo val->id ?>">
Then your code will :
$(".example").change(function() {
if($(this).prop("checked")) {
var value = $(this).val();
var id = $(this).data("id")
console.log(value);
console.log(id);
}
});
I am working on project where questions can have either single choice, or multiple choice, and is fetching from server using ajax. PHP code is
<?php
if($options){
foreach($options as $option){
?>
<li class="col-lg-4 form-group">
<?php
if($choice_type AND $choice_type==1):
?>
<input type="radio" id="options" required value="<?php echo $option->id;?>"> <?php echo $option->option_name;?>
<?php
else:
?>
<input type="checkbox" id="options" required value="<?php echo $option->id;?>"> <?php echo $option->option_name;?>
<?php
endif;
?>
</li>
<?php
}
}
?>
and i was using jquery code
$(document).ready(function() {
$('#submit_next').click(function () {
var qsn = $('#question_id').val();
var opsn = $('#options').val();
var data='que='+qsn+'&ans[]='+opsn;
$.ajax({
type:"GET",
url:"<?php echo base_url('questions/save_answer')?>",
data:data,
success:function(html) {
$("#question").html(html);
}
});
return false;
});
});
Which was working totally fine when i just had radio button, but now i am having both so i need to change code which i thought to be like this.
$(document).ready(function() {
$('#submit_next').click(function () {
var qsn = $('#question_id').val();
var opsn = [];
$("#options").each(function() {
if($(this).attr('checked') || attr('selected')) {
opsn.push.($(this).val());
}
});
var data='que='+qsn+'&ans='+opsn;
$.ajax({
type:"GET",
url:"<?php echo base_url('questions/save_answer')?>",
data:data,
success:function(html) {
$("#question").html(html);
}
});
return false;
});
});
All i want is, if its radio button, i want to receive data on server end in array with single value, or if i have check-box, i will be getting array with multiple values.
For example:
for radio button, i wish
$option[0]= 2;
and for check-box, it should be
$option[0]=>2,$option[1]=>5.
Help will be deeply appreciated.