jquery.load("something.php") not working well - php

i have a something.php which will print the option of dropdownlist,\
then i load the .php file with
$("#dropdownlist").load("something.php");
after i load, the output is displayed, but when i change the selected value, in debug mode i din not saw a selected="selected" on the option of dropdownlist and i cannot set a selectedvalue to the dropdownlist also with
$("#dropdownlist").val("2");
any one know why this happen and how can i solve it?
add-on code
--print option on .php--
while (isset($StatusArr[$loopCount])) {
if ($loopCount == 0) {
$selected = "selected='true'";
} else {
$selected = "";
}
echo "<option value='"
. $StatusArr[$loopCount][0]
. "' "
. $selected
. " >"
. $StatusArr[$loopCount][1]
. "</option>";
$loopCount ++;
}
---call in .js----
$('#select').load("../something.php", function (respond, fstatus, xhr) {
if (fstatus === "success") {
if (status !== "missing") {
$('#status').prop("selectedIndex", 3);
}
} else {
alert("Load data " + fstatus + "\n" + xhr.status + " " + xhr.statusText);
}
});

$("#dropdownlist").prop("selectedIndex", 1);
This jQuery code sets the selected option of the dropdownlist to the 2nd option.

Related

How do I make a column inside select option in HTML/PHP?

So, I have a select option HTML where the data is got from json_decode(file_get_contents("myAPI.com")) and it looked like this:
note : the - is how I divide each data manually.
As you can see, the result is not neat, so tight, and hard to read.
I've tried using Javascript like this :
var spacesToAdd = 5;
var biggestLength = 0;
$("#pks_bank option").each(function(){ var len = $(this).text().length;
if(len > biggestLength){
biggestLength = len;
} }); var padLength = biggestLength + spacesToAdd;
$("#pks_bank option").each(function(){
var parts = $(this).text().split('+');
var strLength = parts[0].length;
for(var x=0; x<(padLength-strLength); x++){
parts[0] = parts[0]+' ';
}
$(this).text(parts[0].replace(/ /g, '\u00a0')+'+'+parts[1]).text; });
But the result is getting worse and farther like this :
Below is my select option code :
<select name='pks_bank' id='pks_bank' data-widget='select2' class='multisel'
style='width:65%; padding-bottom:5px;padding-top:5px;'>
<option value='no'>- Pilih PKS Bank - </option>
<?php
foreach ($list_pks_bank as $data) {
$id_wok = $data->PKS1 . '/' . $data->PKS2 . '/' . $data->PKS3 . '/' . $data->PKS4;
$wok = $data->NMBNK . ' - ' . $data->NOPKS1 . ' - ' . $data->NOPKS2;
echo "<option value='" . $data->PKS2 . "'>" . ucwords(strtoupper($wok)) . "</option>";
}
?>
</select>
How do I make a column inside select option in HTML/PHP? So it would be like this :
In .NET I can give the Combo Box Column > ListBoxColumn property

PHP calculator increase

I am creating simple calculator. I am just learning php and this is a small project.I have created a calculator with two inputs but I am now testing it with just one. It works but only if you type number+number. It doesn't work if it is number+number+number.
I would like that it would work if you inputted 2+2+2... or 2*2*2... or 6-2-2... and 2/2/2...
Code:
// Create Variables
$y = $_POST["input1"];
// Echo input value on screen
echo "<p>Operation: " . $y . "</p>";
// Validation
if(empty($y)){
?>
<script>
$(document).ready(function(){
$('#error').append('Error: Your Input is empty');
});
</script>
<?php
}elseif(preg_match("/[a-zA-Z]/", $y)){
?>
<script>
$(document).ready(function(){
$('#error').append('Error: You can only input numbers');
});
</script>
<?php
}else{
// Calculation Brain FOR + Operator
if (strpos($y,'+') !== false) {
$omega = substr($y, 0, strpos($y, '+'));
$alpha = substr($y, strpos($y, '+') + 1);
echo "<p>Omega: " . $omega . "</p>";
echo "<p>Alpha: " . $alpha . "</p>";
$gamma = $omega + $alpha;
// The Sum FOR + operator
echo "Calculation: " . $gamma;
}
}
That's usually a bad practice, but here you can use eval. But you have first to check that your string doesn't contains disallowed characters.
$allowedCharacters = "0123456789./*-+()% ";
if(preg_match('/^[^'.preg_quote($allowedCharacters).']+$/'), $y) {
eval('$result = '.$y.';');
echo "Calculation: " . $result;
}
The only problem is that you'll not be able to handle errors.
you could use this for the same operation
$test='2+2*3';
eval('$calc = '.$test.';');
echo "Calculation: " . $calc;
the out should be : 8

I have a checkbox that is a bit in sql 2008 server; when it's checked it will run a stored proc

like so:
if (post('tranfer') == 1 && $patient->exists)
{
sql::query("exec event.dbo.sp_tran " . $patient->hex);
}
when it's unchecked it should run a reverse stored proc.
since it's a new check box in the table..none of the members will be checked automatically, they will all have 0.
so i'm afraid i can't use a simple else statement..
else (post('tranfer') == 0 && $patient->exists)
{
sql::query("exec event.dbo.sp_reverse " . $patient->hex);
}
would that work or am i missing something detrimental to this piece?
if ($patient->exists) {
if (post('tranfer') == 1) {
sql::query("exec event.dbo.sp_tran " . $patient->hex);
} else {
sql::query("exec event.dbo.sp_reverse " . $patient->hex);
}
}
UPDATE:
To detect when someone has changed the value of the checkbox, put a hidden field in the form that contains the old value of the column:
echo '<input name="old_tranfer" type="hidden" value="' . $patient->tranfer .'">';
Then when processing the form, you can do:
if (post('transfer') != post('old_transfer') && $patient->hex) {
if (post('tranfer') == 1) {
sql::query("exec event.dbo.sp_tran " . $patient->hex);
} else {
sql::query("exec event.dbo.sp_reverse " . $patient->hex);
}
}

jquery add to array then submit and pass data not working

My problem is:
I'm trying to submit an array of hidden input types, which are stacked into an array using jquery onclick, to a PHP file. However, when I try to count or even echo the passed variable in the php file (saveTest.php), no data appears or the count variable is zero.
I've searched and I found this guy's question:
pass an array from jQuery to PHP (and actually go to the page after submit)
I think I'm close to the above post but I'm still a newbie in jQuery so I don't understand much of the codes.
This is my jquery:
$(function(){
$("td").click(function(){
if($(this).hasClass("on"))
{
alert("Already marked absent");
}
else
{
$(this).addClass("on");
var currentCellText = $(this).text();
$("#collect").append("<input type='text' hidden = '" + currentCellText + "'/>" + currentCellText);
}
});
$("#clicky").click(function(){
$("td").removeClass("on");
$("#collect").text('');
$("#collect").append("Absentees: <br>")
});
});
<?php
session_start();
include 'connectdb.php';
$classID = $_SESSION['csID'];
$classQry = "SELECT e.csID, c.subjCode, c.section, b.subj_name, e.studentID, CONCAT(s.lname, ', ' , s.fname)name
FROM ENROLLMENT e, CLASS_SCHEDULE c, STUDENT s, SUBJECT b
WHERE e.csID = c.csID
AND c.csID = '" . $classID . "'
AND c.subjCode = b.subjCode
AND e.studentID = s.studentID
ORDER BY e.sort;";
$doClassQry = mysql_query($classQry);
echo "<table id='tableone'>";
while($x = mysql_fetch_array($doClassQry))
{
$subject = $x['subj_name'];
$subjCode = $x['subjCode'];
$section = $x['section'];
$studentArr[] = $x['name'];
$studentID[] = $x['studentID'];
}
echo "<thead>";
echo "<tr><th colspan = 7>" . "This is your class: " . $subjCode . " " . $section . " : " . $subject . "</th></tr>";
echo "</thead>";
echo "<tbody>";
echo "<tr>";
for($i = 0; $i < mysql_num_rows($doClassQry); $i++)
{
if($i % 7 == 0)
{
echo "</tr><tr><td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
}
else
{
echo "<td id = '". $studentID[$i] . " '>" . $studentArr[$i] . "</td>";
}
}
echo "</tr>";
echo "</tbody>";
echo "</table>";
?>
This is my php file (saveTest.php)
<?php
$absent = $_POST['absent'];
//echo "absnt" . $absent[] . "<br>";
echo count($absent);
?>
Add name to hidden field:
$("#collect").append("<input type='hidden' name="absent[] value= '" + currentCellText + "'/>" + currentCellText);
It looks like you want to submit a javascript array to a php script and then make use of it. You can make use of .each() function to loop through all the hidden values and adding them into the array. Then use $.post to submit the array to a php script.
<script src="jquery.js"></script>
<script>
$(function(){
$('#btn_submit').click(function(){
var array_hidden = [];
$('input[type=hidden]').each(function(index){
var current_value = $.trim($(this).val());
array_hidden[index] = current_value;
});
$.post('arraysubmit.php', {'hidden_array' : array_hidden}, function(data){
$('#results').html(data);
});
});
});
</script>
<?php for($x=0; $x<=10; $x++){ ?>
<input type="hidden" name="name[]" value="Name<?php echo $x; ?>">
<?php } ?>
<input type="button" id="btn_submit">
<div id="results"></div>
You can then access the array in the php script using the post variable and do whatever you want with it:
$_POST['hidden_array']

selected option in select dissappears on reload

Once it loads in my page, if nothing has been saved in the DB table, all options are shown. As soon as i make a selection and reload the page, the selected option dissapears from the list and isn`t reloaded in the dropdown. Instead, it displays the next value which takes the place of the selected one.
if i check the SQL statement and the $str, it does load all the options except the one which is selected which is in $getBris (it has a value).
What could be causing my select to not display my selected option and instead removing it from the list?
*It specifically doesnt work in IE8, wasnt working in Firefox but now it does
<script src="validation.js" type="text/javascript"></script>
<html>
<body onLoad="checkSecondValue();">
</body>
</html>
<?php
//retrieve all the bris for the drop down
include '../../inc/database.php';
// ORDER BY RAND()
$res = BbqcDatabase::getInstance()->doQuery('SELECT * FROM T_TOURNOI_BRIS');
$str = "<select name='ddlBrisSelected' id='ddlBrisSelected' onChange='checkSecondValue()'>";
$getBris = $_GET['bris'];
$getBris = $getBris - 1;
print_r("bris is : "+ $getBris);
if($getBris == null)
{
$str .= "<option value='' selected></option>";
}
else
{
$str .= "<option value='999'>Choisir un bris</option>";
}
$i = 0;
while($data = mysql_fetch_assoc($res))
{
if($data['F_BRISID'] == $getBris)
{
$str .= "<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>";
}
else
{
$str .= "<option value='" . $data['F_BRISID'] . "'>" . $data['F_BRISTITLE'] . "</option>";
}
$i++;
}
if($getBris == 12)
{
$str .= "<option value=12 selected>Autre</option>";
}
else
{
$str .= "<option value=12>Autre</option>";
}
$str .= "</select>";
echo $str;
if(is_numeric($bris))
{
echo "<script type=\"text/javascript\">alert('test');checkSecondValue();</script>";
}
?>
Use your browser's View Source feature to inspect the actual HTML you are generating (which is, in fact, the only see the browser ever sees). It looks like you're inserting random single quotes.
Update:
<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>"
... will render as:
<option value='blah' selected '>blah</option>
It's the only error I've cared to spot but an HTML validator should find them all. Also, I recommend you use this syntax:
<option value="blah" selected="selected">blah</option>
A construct like this
if($getBris == 12)
{
$str .= "<option value=12 selected>Autre</option>";
}
else
{
$str .= "<option value=12>Autre</option>";
}
is highly wasteful of space and forces you to duplicate a big chunk of html whose only difference is the "selected" attribute. Why not do something like this:
$selected = ($getBris == 12) ? ' selected' : '';
$str .= "<option value=12{$selected}>Autre</option>";

Categories