if checkbox is checked set status to OK - php

Hey guy i have following problem:
I have this line of code which includes a checkbox:
echo('<td bgcolor="'.$Color.'"><input type="checkbox" name="altgeraet" value="'.$resultarray['Status_Altgeraet'].'"></td');
Now i want when the checkbox is selected the mysql value of Status_Altgeraet is set to OK if not it should be set to NOK. At the bottom of my page there is a button when then someone klicks it it goes to my next php file where all data is processed. Here is the point where the mysql value shoul be updated. I have tried following to get the checkbox in to an variable to work with it but it seems not to work:
$Altgeraet = $REQUEST['Status_Altgeraet']));
I am no php expert just an beginner. Hope you guys can give me some tips to solve it :)

$Altgeraet = isset($REQUEST['altgeraet']) ? 'OK' : 'NOK';
Or in a more familiar syntax:
if (isset($_REQUEST['altgeraet'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
If the checkbox is not checked, it will not be present in $_REQUEST/$_GET/$_POST at all.

Related

php/html input checkbox's

Not that sure if this question has been answered somewhere.. have searched for it and found some stuff about it.. but sincerelly don't really understand the answers ive seen and probably am making a major confusion since im not that experienced on working with these languages..
The thing is that i have a group of 5 checkbox's which i want to post in a binary output for example: [0,0,1,1,0].
So far i've managed this by applying the hidden input solution (which i described in a more static way.. since i have a lot of things coming from the db, and other things that dont make sence to explain for this problem.. i think)
$checkychecky = explode(",", [1,0,1,0,0] );
<input type="hidden" name="'.$row_perguntas[0].'[]" value="0"><input type="checkbox" onclick="this.previousSibling.value=1-this.previousSibling.value"'.$checked = ($checkychecky[0] == 1 ? "checked=\"checked\" " : "")/>'.$questionV[1].'
row_perguntas-> comes from the db which gives me the display name
checkychecky -> temp var which stores the values present in the values array [1,0,1,0,0]
questionV -> db label content
which is working just fine.. the problem is when i go forward to another form, and then come back to this form, i get the right arrray values, managed to place the visible checkbox checked, but aint sure on what to do with the hidden field.. since tecnically it isnt set, i dont press the checkbox.. so the onclick doesnt fire.. and when i advance to the next form again i end submiting [0,0,0,0,0]
How should i address this stituation? have tryed to use the.. checkychecky on the hidden input:
<input type="hidden" name="'.$row_perguntas[0].'[]" value="0"'.$value= ($checkychecky[0] == 1 ? "value=\"0\" " : "1")
but no success at all :(
Any ideas? Thanks
Solved..
var checkBQ = ["c0q402","c1q402","c2q402","c3q402","c4q402","c0q408","c1q408","c2q408","c3q408","c4q408"];
var checkBQLength = checkBQ.length;
for (var i = 0; i < checkBQLength; i++) {
if(document.getElementById(checkBQ[i]).checked){
document.getElementById(checkBQ[i]).previousSibling.value = 1;
}else{
document.getElementById(checkBQ[i]).previousSibling.value = 0;
}
}

check the value returned by array_rand?

I'm trying to get some PHP to check if a specific value is pulled out of an array, but I'm having trouble getting it to work. It actually causes the site to go black.
<?php
$message_array = file("http://www.example.com/wp-content/themes/mytheme/Subtitles.css");
$message = array_rand($message_array);
echo "$message_array[$message]";
$GfCheck = "<audio id='audio' src='example.com/wp-content/uploads/2016/01/example.wav'; preload='auto' ></audio><a onclick='GFFUNC()'><img src='example.com/wp-content/uploads/2016/01/Gf.png'; height='90px' alt='gf' title='gf'/></a>";
if ($message_array[$message] == $GfCheck) { $Gf = "1" } else { }
?>
the $Gf would then in turn add a secret section to a menu later on.
Can anyone help me figure out what's going wrong?
Thanks in advance for your help!
Answer largely came from Rizier123's comments on the main post.
While the file tag did not work, the code did in the end. I am left with an issue of global variables now, but the hard part is done!

Php: Session not working correctly

i have code:
$search = $_REQUEST['search'];
if(!isset($_REQUEST['search'])){
if(is_null($_SESSION['ss_search']))
$search = 0;
else
$search = $_SESSION['ss_search'];
}
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
firstly I enter number 1 and submit, browser return 1. Then I enter number 2 and submit, browser return 2 but I refresh browser, it return 1. I don't know why session stored value 2 but when refresh it return old value. i used session_start() on top.
Please try this code
$search = $_REQUEST['search'];
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
Try this and let me know.
I am not sure but try to remove (NOT)! from the if statement. It worked for me.
change
if(!isset($_REQUEST['search'])){
to
if(isset($_REQUEST['search'])){
Please let me know if you face any problem.
I have tested your code and amend it as following:
$search ="";
if(!isset($_REQUEST['search'])){
if(is_null($_SESSION['ss_search']))
$search = 0;
else
$search = $_SESSION['ss_search'];
}else{
$search=$_REQUEST['search'];
}
$_SESSION['ss_search']=$search;
echo $_SESSION['ss_search'];
using $_REQUEST['search'] without checking if it is set, cause it to generate undefined index notice. so now $_REQUEST['search'] is called only when it is set, Another cause is if you have set the default value on your form input field equal to 1, and when you refresh the page and allow it to resend the form, it may send the default value of your form.

Javascript prompt() output to PHP function

I've been trying to get user input from a Javascript prompt into a PHP function and have run into a lot a walls doing so. I am at a loss trying jQuery's $.post method- as the PHP simply does not want to execute, not sure why.
Anyways, here is a gist of what I am doing at the moment:
1 A project and its data are loaded from a database- this info is displayed in a table.
2 All data in the table is editable via Javascript prompt(), the code I am using for this is below:
<div id="lvl3"><?php echo $fetchdata['name']; ?></div>
The above work as such: lvl3 is a tag for font styling; blank href to make it act as a link; popupprompt is the prompt function I made, it takes one argument, the 'type' or what is being edited (1 for project name, 2 for project description, ect); return false so the page doesn't reload; php echo to display project data in the table.
3 Once the user clicks the object above- it executes a javascript function called popupprompt taking an argument of 'type', or what project info is being changed. the code for this function is below:
function popupprompt(type) {
switch(type)
{
case 1:
var name = prompt("Project Name:", "");
if (name != null && name != "")
{
//Change Project Name
var getname = name;
var gettype = type;
$.post("edit.php", { type: gettype, name: getname });
} else if (name == "") {
senderror("Please enter a valid Project Name");
} else {
//Prompt canceled
sendnotification('Canceled my ass!');
}
break;
case 2:
//Description?
case 3:
//Version?
case 4:
//Release?
default:
alert("There was an error processing your request.");
break;
} }
Th issue I am having in this function is that nothing in edit.php is executed- and i haven't the slightest clue why. Also, i've had to change the brackets around so it shows properly in the code box- so don't mind those.
4 Anyways, now user input is posted to edit.php- which doesn't work, but i'll post it anyways:
<?php
$type = $_POST['type'];
$name = $_POST['name'];
switch($type) {
case 1:
dbconnect();
$urlext = geturlext();
$authenticated = isauthenticated();
if ($authenticated == false)
{
echo("<script>senderror('Access denied');</script>");
} else {
//Escape and trim input
$input = trim($input);
$input = mysql_real_escape_string($input);
$update = "UPDATE 'projects' SET 'name' = '$input' WHERE 'name' = '$urlext'";
$updatequery = mysql_query($update) or die(mysql_error());
echo("<script>sendnotification('Project Name updated');</script>");
}
break;
default:
break;
}
?>
Again had to move some brackets around. But anyways- this function is supposed to update the data in the database- however, it instead does nothing. I've placed alerts in the beginning and they are never called.
Anyways, long story short- if you know what i'm doing wrong please enlighten me, also, if there is a better way to do this- please let me know!
I appreciate all help,
Thanks
Figured it out after a good week of setting it aside. Took a long look at it in firebug, rooted out a typo that was causing most of my issues and also a MySQL syntax error.
I don't know why, but my custom error reporting system does not work when called from a file in this manner. It might be another thing i'm overlooking but firebug sure helped.
Thanks to those who tried to help <3

PHP + MsSQL = Checkbox problems. Need HELP!

I have been working on this one topic for weeks. I'm creating a webpage that pulls information from MsSQL server. My problem is where there is a section with few check boxes.
The checkboxes are suppose to be checked if they are found in the SQL database.
If the borrower used money from "Savings", "Checking" and "Stock" those checkboxes should be checked in HTML page. In my case it is only checking whatever is on the first row of the SQL search list. So if the list has "Saving" on the first row, only the "Saving" checkbox will be checked not the rest on the list. I tried using loop (while($r->EOF)), but then it picks whatever is on the end of the list. Here is what I am using to pull data from the SQL server. Thank you in advance for your help. Really appreciate it!
function __construct($_ldid, $_lrid)
$this->ldid = $_ldid;
$this->lrid = $_lrid;
//$loan is defined in the PHP.class (which is shown below).
$q = ("SELECT * FROM Tabel_name where loan_id = 885775")
$v = array($_ldid, $_lrid);
$r = $db->Execute($q, $v);
$this->downpaymenttype = $r->fields; //<- I think I have this line done wrong because it is suppose to store the outputs to an array.
//So wherever the "$loan" is in HTML page, it will take the outputs from the above statement.
//The above code, which is in PHP.class file, outputs the following(or suppose to):
1 885775 Checking
2 885775 Saving
3 885775 Gift
In the HTML webpage, the following codes should check mark the boxes according to the outputs:
<input type="checkbox" name="DPS1[]" id="DPS1-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Checking"} checked {/if}/>
<input type="checkbox" name="DPS2[]" id="DPS2-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Saving"} checked {/if}/>
<input type="checkbox" name="DPS3[]" id="DPS3-{$key}" {if $loan-> downpaymenttype.downpaymentsource == "Gift"} checked {/if}/>
Only the "checking" checkbox is getting checked because that's the one on the first row.
I also tried to loop, but it is not storing in array (I guess I don't know how to use the array for session)
$q = ("SELECT Tabel_name FROM loan_downpaymentsource where loan_id = 885775");
$v = array($_ldid, $_lrid));
$r = $db->Execute($q, $v);
while(!$r->EOF) {
$this->downpaymenttype = $r->fields;
$r->MoveNext();
}
Add a loop to go through your results before displaying and set flags. $r->fields is only going to grab the fields for the current row. You need to go through your entire rowset to see if the others are set.
Something like (It looks like you're using adodb or something like that)
$checking = false;
$saving = false;
$gift = false;
while(!$r->EOF){
$row = $r->fields;
switch($row->downpaymenttype){
case 'Checking':
$checking=true;
break;
case 'Saving':
$saving=true;
break;
case 'Gift':
$gift=true;
break;
default:
break;
}
$r->moveNext();
}
Now on your view check the flags. I've not used smarty but you'll have to pass those flags to your template as well as I'm sure you know, and then change your checkbox lines to check if each flag is true/false.
Hope that helps!

Categories