How to Get Variables using Pop Ups in PHP? - javascript

I am new in PHP. I have two files. file_upload.php and test1.php i have a button on file_upload.php
<input type="button" value="Link More Opinion" onclick="popUp('test1.php');" />
JavaScript of that Button
<script type="text/javascript">
function popUp(url) {
window.open(url,'PHP Pop Up','width=500,height=500');
}
</script>
When i click on Button test1.php file is open in Pop Up window contain checkbox
Here is code
<?php
include "connection.php";
$sql= "SELECT * FROM `og_companies` ORDER BY name";
$result = mysql_query($sql) ;
while($row = mysql_fetch_array($result))
{
$all_opinions[] = $row['name'];
}
$all_opinions_implode = implode(",", $all_opinions);
$all_opinions_explode = explode (",", $all_opinions_implode);
?>
<form action="file_upload.php" id="form" method="post" name="form">
<?php
for ($i=0; $i<count($all_opinions_explode); $i++) {
echo "<input type='checkbox' name='test_link' > $all_opinions_explode[$i]";
//echo ;
echo'</br>';
}
?>
<input type="submit" name="submit" id="submit" value="submit"/>
Now i want to get Value of Checkbox on file_upload.php but i fail. Can anyone please help me?

you can solve your problem by using fancybox to open inline element!
I put both of your pages in one file and then I use fancy box to open #form1 element when you click on the button.
then you can check the checkboxes and see the result;
checked values printed to #print-values div element.
$(document)
.ready(function() {
$(".various").fancybox({
maxWidth: 800,
maxHeight: 600,
fitToView: false,
width: '70%',
height: '70%',
autoSize: false,
closeClick: false,
openEffect: 'none',
closeEffect: 'none'
});
$('.button1').click(function(e) {
e.preventDefault();
$('.various').click();
});
})
.on('change', '[name=test-link]', function() {
$('#print-values').empty();
$('#print-values').append("<span>You've checked:</span><br />");
$('[name=test-link]').each(function() {
if ($(this).prop("checked")) {
var v = $(this).val();
$('#print-values').append(v + "<br />");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.pack.js"></script>
<a class="various" href="#form1" style='display: none;'></a>
<input class='button1' type="button" value="Link More Opinion" />
<div id='print-values'></div>
<div style='display: none;'>
<form id='form1'>
<input type='checkbox' name='test-link' value="1" />
<span>1</span>
<br />
<input type='checkbox' name='test-link' value="2" />
<span>2</span>
<br />
<input type='checkbox' name='test-link' value="3" />
<span>3</span>
<br />
</form>
</div>

Related

How to change radio button into checkbox and still insert data into database (check/uncheck)

I'm not a dev, I never do it but for a specific thing I've made a web page where people can insert data into a sql database.
My code is working but I guess I can optimize it because I've made it from things I've found over the Internet and I would like to turn radio buttons into check boxes because it is a lot of buttons at the end when there are many lines.
Maybe there are things wrong because I have change names so there are no ref to my company.
<!DOCTYPE html>
<?php
include('Connexion_DB.php');
/* Queries */
if (isset($_POST['Btn_Mode1'])){
$req1 = "UPDATE DATABASENAME SET Mode1 = '".$_POST['Mode1']."' WHERE ID = '".$_POST['ID']."'";
$result1 = sqlsrv_query($con,$req1);
}
if (isset($_POST['Btn_Mode2'])){
$req2 = "UPDATE DATABASENAME SET Mode2 = '".$_POST['Mode2']."' WHERE ID = '".$_POST['ID']."'";
$result2 = sqlsrv_query($con,$req2);
}
if (isset($_POST['form1']))
{
$Status = 'Y';
$array = explode(PHP_EOL, $_POST['textareaname']);
foreach($array as $index=>$value) {
if($value === '') unset($array[$index]);
}
foreach ($array as $key => $value) {
$req = "INSERT INTO DATABASENAME (DATA, Active, Mode1, Mode2) VALUES ('".$value."','".$Status."', 'false','false');";
$result = sqlsrv_query($con,$req);
}
echo "<pre>";
echo print_r($array);
echo "</pre>";
}
if (isset($_POST['form2'])) {
foreach ($_POST['ID'] as $id) {
$req3 = "DELETE FROM DATABASENAME WHERE ID = '".$id."'";
$result3 = sqlsrv_query($con,$req3);
}
}
include('menu.php');
?>
<html>
<head>
<title>TITLE</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
</head>
<script>
$(function() {
$('.RadioBtn_Mode1').change(function() {
Mode1 = $(this).val();
ID = $(this).attr('id');
$.post( "Test_Page.php", {
Btn_Mode1: true,
ID: $(this).attr('id'),
Mode1: $(this).val()
} ).done(function( ) {
toastr.success(ID +' est a ' +Mode1);
});
});
});
$(function() {
$('.RadioBtn_Mode2').change(function() {
Mode2 = $(this).val();
ID = $(this).attr('id');
$.post( "Test_Page.php", {
Btn_Mode2: true,
ID: $(this).attr('id'),
Mode2: $(this).val()
} ).done(function( ) {
toastr.success(ID + ' est a ' +Mode2);
});
});
});
</script>
<body>
<div class="container body-container">
<div id='titre'class='titre'><h1>Insert Data</h1></div>
<form method='post' >
<input type="hidden" name="form1" />
<br />
<p>Data :</p>
<textarea name="textareaname" cols="60" rows="5"></textarea>
<br />
<br />
<input type="submit" value="Submit"/>
</form>
<form method="post" action="Test_Page.php">
<input type="hidden" name="form2">
<input type="submit" id="deleteBtn" value="Delete"/>
<div class="nav" id="inst1">
<table class="one">
<thead>
<tr>
<th scope="col"></th>
<th scope="col">Active</th>
<th scope="col">Data</th>
<th colspan="2">Mode1</th>
<th colspan="2">Mode2</th>
</tr>
</thead>
<tbody>
<?php
$req1 = "SELECT ID, Active, Data, Mode1, Mode2 FROM DATABASENAME" ;
$result1 = sqlsrv_query($con,$req1);
while($row1 = sqlsrv_fetch_array($result1))
{
echo "<tr>";
echo "<td><input type='checkbox' name = 'ID[]' value='" . $row1['ID'] . "'/></td>";
echo '<td>'.$row1['Active'].' </td>
<td>'.$row1['Data'].' </td>';
if ($row1['Mode1']=='False'){
echo '<td>False : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="False" checked/></td>';
echo '<td>True : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="True"/></td>';
} else {
echo '<td>False : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="False"/></td>';
echo '<td>True : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="True" checked/></td>';
}
if ($row1['Mode2']=='False'){
echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False" checked/></td>';
echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True"/></td>';
} else {
echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False" /></td>';
echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True" checked /></td>';
}
echo
'</td>
</tr>';
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>
<?php
sqlsrv_close($con);
?>
I would like a master to tell me what I can do better and help me to turn radio buttons into check boxes for the data insert.
SQL format table is BIT, so I want to insert true / false when I check/uncheck like I do with buttons.
Thanks !
Without rewriting all of the code in the answer
The idea of a checkbox, is marked or not. The input passed to the server by the browser, passes only if the checkbox is marked
So the the checkbox PHP code should check whether isset($_POST['RadioBtn_Mode2'] . $row1['ID']);
A simple reference here
Which is pretty much what you do already
All that's left is changing the echoing of the html, basically dropping one tag per
radio button, and changing the type to checkbox
For example, from this:
echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False" /></td>';
echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True" checked /></td>';
To This:
echo '<td>Check: <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="checkbox" value="False" /></td>';
Once that works, changing the universal name RadioBtn to CheckBx (or to anything else) is esthetics, of course

How to check and uncheck all checkboxes

I have a problem with my code. I am trying to click on the button to check all of the checkboxes but nothing is working.
Here's my code.
<body>
<html>
<input type="checkbox" id="chk_boxes1" name="chk_boxes1" class="delete_customer" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<input type="checkbox" id="chk_boxes1" name="chk_boxes1" class="delete_customer" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<input type="checkbox" id="chk_boxes1" name="chk_boxes1" class="delete_customer" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<button type="button" name="btn_checkall" id="btn_checkall">Check all</button>
</html>
</body>
<script>
$(document).ready(function(){
$('#btn_checkall').click(function(){
if($(this).prop('checked')){
$('.chk_boxes1').prop('checked', true);
}else{
$('.chk_boxes1').prop('checked', false);
}
});
});
</script>
I don't know where the trouble is coming from, but I guess that something have to do with the id, name or whatever it is that make it stop working.
Can you please show me the correct code that I could use to check and uncheck all of the checkboxes when I click on a button using with my code?
One thing to correct is that you have your html tag after your body tag. The html tag should for sure be the parent.
Other than this it looks like you have assigned "chk_boxes1" to id's instead of classes. If you change them to classes then things should work:
<input type="checkbox" name="chk_boxes1" class="delete_customer chk_boxes1" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<input type="checkbox" name="chk_boxes1" class="delete_customer chk_boxes1" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<input type="checkbox" name="chk_boxes1" class="delete_customer chk_boxes1" value="<?php echo $rowTrackers['id']; ?>" />
<br>
Also you are checking at button to determine checked status. This also will make the code not work. You can add and remove a class to the button if you want to change status or you can do something like this to check the group of checkboxes:
$(document).ready(function(){
$('#btn_checkall').click(function(){
if ($('.chk_boxes1:checked').length == $('.chk_boxes1').length) {
$('.chk_boxes1').prop('checked', false);
}
else {
$('.chk_boxes1').prop('checked', true);
}
});
You have a lot going wrong there.
Firstly, your HTML tags are in the wrong order. Your document should start with <html> and end with </html>:
So, change your document to be like this:
<html>
<body>
// your content
</body>
</html>
And you also want to include your script at the bottom of your <body> or, as you're listening for DOM ready event, include the script in the <head>. So, adjust your <script> position to here:
<html>
<body>
// your content
<script>
// script here
</script>
</body>
</html>
As for the main question, I suggest these changes:
HTML:
Remove the duplicate IDs. ID are meant to be totally unique for each element.
<input type="checkbox" name="chk_boxes1" class="delete_customer" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<input type="checkbox" name="chk_boxes1" class="delete_customer" value="<?php echo $rowTrackers['id']; ?>" />
<br>
<input type="checkbox" name="chk_boxes1" class="delete_customer" value="<?php echo $rowTrackers['id']; ?>" />
<br>
jQuery:
Select the checkboxes by name instead of ID:
$(document).ready(function() {
$('#btn_checkall').click(function(){
var $chk_boxes = $('input[name="chk_boxes1"]');
if($chk_boxes.prop('checked')) {
$chk_boxes.prop('checked', true);
} else {
$chk_boxes.prop('checked', false);
}
});
});
As people have said, ids should be unique:
$(document).ready(function(){
//Use JQuery .on to attach event handler
$('#btn_checkall').on('click', function(){
//Get all checkboxes by class
const $checkboxes = $('.delete_customer');
//Use .each to iterate the checkboxes
$checkboxes.each(function(){
$(this).prop('checked', true);
});
});
});

Form submit in jquery dialog box not working

In the following code, both buttons of dialog box is meant to submit form. But its not submitting when buttons from dialog are clicked. If user chose yes we need to send a value to the posting page. please help me with this.
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="edit_billto" id="edit_billto">
<input type="hidden" name="option" value="com_phpshop" />
<input type="hidden" name="page" value="<?php echo $next_page; ?>" />
<input type="hidden" name="next_page" value="<?php echo $next_page; ?>" />
<input type="hidden" name="Itemid" value="<?php echo $Itemid; ?>" />
<input type="hidden" name="autoship_flag" id="autoship_flag" value="0">
<input type="submit" id="bill_sumbit" class="button" name="submit" value="<? echo _E_SAVE ?>" <? echo $submit; ?> />
</form>
Jquery part
<div id ="alert" style="display:none"> <p> This address is used in autoship.
You need to change the address in autoship too?
</p></div>
<script>
$(document).ready(function() {
$("#alert").dialog({autoOpen: false,
modal: true,
buttons: {
No : function(){
isconfirm = true;
//$("form[name='edit billto']").unbind('submit');
$("#edit_billto").submit();
$(this).dialog("close");
//$("form[name='edit billto']").unbind('submit').submit();
},
Yes: save_info
},
resizable: true,
height: "auto",
width: "auto",
closeOnEscape: false,
title: "Autoship Update"
});
});
var isconfirm = false;
$( "form[name='edit_billto']" ).submit(function(e) {
//alert(isconfirm);
if(isconfirm === true){
return true;
}
if(isconfirm === false){
e.preventDefault();
$( "#alert" ).dialog( "open" );
}
});
function save_info(){
isconfirm = true;
$("#autoship_flag").val("1");
$("form[name='edit_billto']").unbind('submit');
$("form[name='edit_billto']").submit();
}
</script>
Not sure why, you're .submit() isn't working but you could try .trigger('click')
Something like this should work:
No : function(){
$("form[name='edit_billto']").off('submit');
$("#bill_sumbit").trigger('click');
}
,

Displaying div on click submit button

I have a radio button and a submit button. On click of this submit button, I need to display a div.
My code is as follows:
<form action="" method="GET" name="myform">
<input type="radio" name="ID" value="total" id="radioInputValue"/>
<input type="submit" name="details1" value="Details"
onclick="return showDiv();"/>
<div id="element_to_pop_up" style="display: none;">
<?php
echo "DETAILS";
if (isset($_GET['details1'])) {
if (isset($_GET['ID'])) {
$n = $_GET['ID'];
echo "VALUE IS = $n";
}
} else {
echo "</br> DETAILS FAILED";
}?>
</div>
</form>
My JavaScript:
function showDiv() {
document.getElementById('element_to_pop_up').style.display = 'block';
return false;
}
Style:
#element_to_pop_up
{
background-color:#fff;
border-radius:15px;
color:#000;
padding:20px;
min-width:400px;
min-height: 180px;
}
I am getting the div on click of the details button. But its not entering the if condition .
Instead its giving the output as:
DETAILS
DETAILS FAILED
I want this as my output:
DETAILS
VALUE IS = total
How can i get this radio button value on clicking submit button, Plz help
just copy paste it: it works fine..
<form action="" method="POST" name="myform">
ONE<input type="radio" name="ID" value="total" id="radioInputValue" />
<input type="submit" name="details1" value="Details" onclick="return showDiv();" />
</form>
<div id="element_to_pop_up">
<?php
echo"DETAILS";
if(isset($_POST['details1']))
{
if(isset($_POST['ID']))
{$n=$_POST['ID'];
echo "VALUE IS = ".$n;
}
else
{
echo"</br> DETAILS FAILED";
}
}
?>
</div>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
function showDiv() {
document.getElementById('element_to_pop_up').style.display = 'block';
// return false;
}
</script>
Change your submit button like this:
<input type="submit" name="details1" value="Details" onclick="showDiv()" />
When you click your button, it displays the div.
Your PHP code should work, but make sure the file is .php and not .html or .htm.

Passing multiple variables to Javascript Function

I am able to pass one variable (ID) to the javascript function, however when I go to add the businessname variable, the popupdiv does not display.
Button to displaydiv and pass ID (this works)
<input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.")'>
Javascript (works)
function displayDiv(divid) {
e=document.getElementById("zbadmin-"+divid);
strUser=e.options[e.selectedIndex].value;
if (strUser=='2') {
document.getElementById('def').style.display = "block";
document.getElementById('link-id').href="delete?id="+divid;
}
}
But when I add businessname, doesnt work
<input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.",'".$businessname."')'>
Javascript (doesnt work)
function displayDiv(divid,divbizname) {
e=document.getElementById("zbadmin-"+divid);
strUser=e.options[e.selectedIndex].value;
if (strUser=='2') {
document.getElementById('def').style.display = "block";
document.getElementById('link-id').href="delete?id="+divid+"&businessname="+divbizname;
}
}
Am I missing something very simple? I replaced it with plain text and it didnt work either.
EDIT:
Heres the code prior to the button. It grabs data from SQL table and produces a dropdown. As a test, ive put $id as both variables in the below code, it works fine and displays the popup.
while($row = mysql_fetch_array($proposal_result)) {
$date=substr($row["date"], 0, 50);
$formatted_date=date('d/m/Y', strtotime($date));
$id=substr($row["idproposal"], 0, 50);
$businessname=substr($row["businessname"], 0, 50);
$status=substr($row["status"], 0, 50);
$staff=substr($row["staff"], 0, 50);
$file_access='<storage-bucket>';
$file_name='proposal_'.$id.'_'.$businessname.'.pdf';
$file=$file_access.$file_name;
print "<tr><td>".$formatted_date."</<td><td>".$id."</td><td width='25px'><a href='".$file."'target='_blank'><img src='".$images."/attachment.png' alt='file'></a></td><td>".$businessname."</td><td>".$staff."</td><td>".$status."</td><td>
<div>
<select id='zbadmin-".$id."' name='zbadmin-".$id."' class='dropdowns' required>
<option value='0'>Select and action...*</option>
<option value='1'>Change Status for ID#".$id."</option>
<option value='2'>Delete Proposal</option>
</select>
<input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.",".$id.")'></div>
If I use the $id, $date it works fine as its grabbing numbers, but as soon as I replace the second variable with one thats a string, it doesnt display the popupdiv below:
<div id='def'>
<div id='popup'>
<form name='deletefeedback' action='' method='post'>
<!--<img id='close' src='images/3.png'> CLOSE ICON-->
<h2>Reason for deleting proposal <script>function getID() { alert($divid);window.location='?divid='+$divid';}</script></h2>
<hr>
<input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Added by mistake<br />
<input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>No longer required<br />
<input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Incorrect Information Provided<br />
<input id='deletereason' type='radio' name='deletereason' class='radio' value='Added by mistake'>Reason 4<br />
<textarea name='deletecomments' placeholder='Comments...'></textarea><br />
<a href='' id='link-id'><input type='button' id='report-submit' value='Delete Proposal'></a><a href='/zerobooks-admin-dashboard'><input type='button' id='report-submit' value='Cancel'></a>
</form>
</div>
</div>
You're adding a weird mix of single quotes before the second argument, get rid of those:
onclick='displayDiv(".$id.", ".$businessname.")'
Got it working. Found the answer at passing a parameter with onclick function is read as a variable when it should be read as a string
Essentially, I just added backslashes to pass parameter as string
<input type='button' id='report-submit' value='Go' onclick='displayDiv(".$id.",\"".$businessname."\")'>
Use
<input type='button' id='report-submit' value='Go' onClick="displayDiv('<?php echo $id ?>', '<?php echo $businessname ?>');">
Example
<?php
$id = '1';
$businessname = 'Business Name';
?>
<html>
<head>
<script type="text/javascript">
function displayDiv(id, businessname) {
alert(id);
alert(businessname);
}
</script>
</head>
<body>
<input type='button' id='report-submit' value='Go' onClick="displayDiv('<?php echo $id ?>', '<?php echo $businessname ?>');">
</body>
</html>

Categories