Hi all i was making a website were you can add excel file and choose a person and when you choose a person then it the web would only print those rows were only is a person that you chosen from select box.
Does anyone know how i could get result variable from js function (function getSelectedPerson()) and that resul insert in php for statement.
<script>
var filling_prices= new Array();
filling_prices["None"]=0;
filling_prices["dla_dei_mat"]="Deividas Matulis";
filling_prices["dla_tom_ver"]="Tomas Veršinskas";
filling_prices["dla_dar_ser"]="Darius Sereika";
filling_prices["dla_dov_pra"]="Dovydas Prakapas";
function getSelectedPerson()
{
var theForm = document.forms["dla_darbuotojo_pasirinkimas_form"];
var selectedPerson = theForm.elements["dla_darbuotojo_pasirinkimas"];
fillSelectedPerson=filling_prices[selectedPerson.value];
return fillSelectedPerson;
}
</script>
<?php
require_once "Classes/PHPExcel.php";
$chosenPerson = $_GET["getSelectedPerson()"];
$tmpfname = "visi.xls";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
if ($chosenPerson == ($worksheet->getCell('D'.$row)->getValue()) ) {
echo "<tr><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><tr>";
}
}
echo "</table>";
?>
<form name="dla_darbuotojo_pasirinkimas_form">
<div class="Choose_people">
<select name="dla_darbuotojo_pasirinkimas">
<option value="None">Darbuotojai</option>
<option value="dla_dei_mat">Deividas Matulis</option>
<option value="dla_tom_ver">Tomas Versinskas</option>
<option value="dla_dar_ser">Darius Sereika</option>
<option value="dla_dov_pra">Dovydas Prakapas</option>
</select>
</div>
</form>
I think this is what you are looking for. You can not use Javascript variables in PHP, you have to pass the values via GET or POST requests. You don't need the script portion of your example.
<form name="dla_darbuotojo_pasirinkimas_form" action="yourscript.php" method="get">
<div class="Choose_people">
<select name="dla_darbuotojo_pasirinkimas" onchange="this.form.submit()>
<option value="None">Darbuotojai</option>
<option value="dla_dei_mat">Deividas Matulis</option>
<option value="dla_tom_ver">Tomas Versinskas</option>
<option value="dla_dar_ser">Darius Sereika</option>
<option value="dla_dov_pra">Dovydas Prakapas</option>
</select>
</div>
</form>
Change
$chosenPerson = $_GET["getSelectedPerson()"];
to
$chosenPerson = $_GET["dla_darbuotojo_pasirinkimas"];
You may also need a condition in your PHP script
if(isset($_GET["dla_darbuotojo_pasirinkimas"])) {
// do the get person stuff
$chosenPerson = $_GET["getSelectedPerson()"];
$tmpfname = "visi.xls";
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
echo "<table>";
for ($row = 1; $row <= $lastRow; $row++) {
if ($chosenPerson == ($worksheet->getCell('D'.$row)->getValue()) ) {
echo "<tr><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('F'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('G'.$row)->getValue();
echo "</td><tr>";
}
}
echo "</table>";
}
So if the script has been called with no person it won't try to run that code.
I currently have a PHP script that works well, but when it comes to adding JavaScript I'm having a small issue.
What I'm trying to achieve is display the results in a php foreach loop which works well, but I have a onclick JavaScript function that is suppose to display results in the same row as the results of the foreach loop, but instead it changes the current value of only one results.
So I currently have four car types with a price. When one is selected I would like the name and price to display on top of the [echo "Select:" . $name] line.
My current code is this:
<?php
$cars = array(
array("Volvo",40.000),
array("BMW",45.000),
array("Saab",50.000),
array("Land Rover",60.000)
);
foreach($cars as $key){
$name = $key[0];
$price = $key[1];
echo "<span id='$name'></span> <span id='$price'></span><br />";
echo "Select: " . $name;
?>
<script type="text/javascript">
function grabData(d){
document.getElementById("<?php echo $price; ?>").innerHTML = d.getAttribute("data-price");
document.getElementById("<?php echo $name; ?>").innerHTML = d.getAttribute("data-title");
}
</script>
<input onclick="grabData(this);" type="radio" name="rate" data-price="<?php echo $key[1]; ?>" data-title="<?php echo $key[0]; ?>"><br />
<?php
}
?>
So if all options are selected the end results should look like this:
Volvo 40
Volvo:
BMW 45
BMW:
Saab 50
Saab:
Land Rover 60
Land Rover:
Any help or direction is highly appreciated. Thanks!
The loop is simply defining the same function over and over. When it gets called, it will call the last definition, not the one for that element in the loop.
You should just define the function once, and add additional parameters. Then you can call it with different parameters for each element.
<script type="text/javascript">
function grabData(d, priceid, nameid){
document.getElementById(priceid).innerHTML = d.getAttribute("data-price");
document.getElementById(nameid).innerHTML = d.getAttribute("data-title");
}
</script>
<?php
$cars = array(
array("Volvo",40.000),
array("BMW",45.000),
array("Saab",50.000),
array("Land Rover",60.000)
);
foreach($cars as $key){
$name = $key[0];
$price = $key[1];
echo "<span id='$name'></span> <span id='$price'></span><br />";
echo "Select: " . $name;
?>
<input onclick="grabData(this, '<?php echo $price; ?>', '<?php echo $name; ?>');" type="radio" name="rate" data-price="<?php echo $key[1]; ?>" data-title="<?php echo $key[0]; ?>"><br />
<?php
}
?>
set the script part as a string in a variable and echo it using php, php sees every <?php ?> as a different coding block.
I need to pass a javascript array, via POST, to a PHP file.
I've tried to semplify my original business trying to explain my troubles ...
This is my first PHP file, in which I declare a javascript array and I set each element to 1 value (not a great business I know, but it doesn't matter, it's only to explain ....)
<?php
echo '<form action="testPhp-2.php" method="POST" target="testPhp-2">';
echo '<script type="text/javascript">';
echo 'var arr_selections = [];';
echo 'for(i=0; i < 10; i++)';
echo ' {';
echo ' arr_selections[i] = 1';
echo ' }';
echo 'arr_selections_json = JSON.stringify(arr_selections);';
echo 'alert (arr_selections[2]);';
echo 'document.write("<br/>");';
echo ' ';
// echo 'document.write("<input type="hidden" />");';
echo 'document.write("<input type=\"hidden\" name=\"arr_selections_json\" value=\"arr_selections_json\" />");';
echo ' ';
echo '</script>';
echo ' <input type="submit" value="Controlla">';
echo ' </form>';
?>
.... and here you are the code of testPhp-2 file ...
<?php
if(isset($_POST['arr_selections_json']))
{
echo "OK, array exist !! ";
echo '</br>';
echo '</br>';
$arr_selections = json_decode($_POST['arr_selections_json'], true);
echo $arr_selections[0];
}
else {
echo "NO; array does not exist !! ";
echo '</br>';
echo '</br>';
}
?>
Now, if you try to execute the code you'll see the OK, array exist !! message but no array value is printed about the echo $arr_selections[0]; line of code in testPhp-2.php file.
Any suggestion will be appreciated! Thank you in advance!
Cesare
Problem is that you're setting the value of the input to the litteral string "arr_selections_json" instead of to the contents of that variable.
Change
echo 'document.write("... value=\"arr_selections_json\" />");';
To
echo 'document.write("... value=\""+arr_selections_json+"\" />");';
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need help to convert this code to codeigniter MVC i have 3 file php Openlink3.php for viewer class.php for class and databaseConnection.php for model
i have problem converting this php file to codeigniter. Please help me.
this my PHP code Openlink.php
<?php
include("DatabaseConnection.php");
include("class.php");
Global $AppCode;
//$AppCode='0001';
$user='0772';
$db = new dbconnection();
$conn = $db->dbOpen();
$misApp = new misApplication();
$sql="SELECT a.RAL_Code,a.RAL_App_Name,a.RAL_Remarks,b.RAL_APPIconLocation,b.RAL_ApplicationLink FROM R_Application_List as a ,R_Application_Links as b where a.RSC_Active=1 and a.RAL_Code=b.RAL_APPCODE";
$rs=odbc_exec($conn,$sql);
$appcount;
echo "<table align=center cellpadding=10>";
echo "<tr height=120>";
$appcount=0;
while (odbc_fetch_row($rs))
{
$appcode=odbc_result($rs,"RAL_Code");
$appname=odbc_result($rs,"RAL_App_Name");
$desc=odbc_result($rs,"RAL_Remarks");
$icon=odbc_result($rs,"RAL_APPIconLocation");
$applink=trim(odbc_result($rs,"RAL_ApplicationLink"));
if($appcount==3){
echo "<tr height=120>";
//echo "<td>";
$appcount=0;
}
//echo "<td>".$appcode."</td>";
//echo "<td>".$appname."</td>";
echo "<td valign=bottom><img src=".$icon."height=30 width=150 onclick='openApp(\"$appcode\",\"$user\",\"$applink\");' </img></td>";
//echo "<td><input type=\"button\" ></td>"
//echo "<td>".$desc."</td>";
echo '<script>openApp();</script>';
$appcount++;
//echo $icon."<br/>";
}
echo "</table>";
?>
<img src="dtr.png" height="30" width="150" onclick=openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $misApp->getAppLink($conn,"0001",$user); ?>") ></img></br>
<input type="button" id="btn.DTR"; name="btn.Dtr"; value="DTR"; onclick='openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $misApp->getAppLink($conn,"0001",$user); ?>")'/>
<input type="button" id="jose"; name="btn.OpenApp"; value="Approval System(test)"; background="dtr.png";onclick='openApp("0014","<?php echo $user; ?>","<?php echo $appLink = $misApp->getAppLink($conn,"0002",$user); ?>")'/>
<script language="javascript">
function openApp(Appcode, User, link){
var OpenLink;
OpenLink = link+"?A1="+User+"&A2="+Appcode; // Concatenating strings
//alert(OpenLink);
//document.write (OpenLink); // printing the Concatenated string
//window.open(OpenLink);
//window.open(OpenLink,'_blank', 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000, top=10000, width=10, height=10, visible=none', '');
window.location.href = OpenLink;
}
</script>
</html>
this my PHP code class.php
<?php
class misApplication{
public function getAppLink($actveConn,$appCode,$user){
$linktoOpen;
$sql="select * from R_Application_Links where RAL_APPCode='$appCode'";
$rs=odbc_exec($actveConn,$sql);
while (odbc_fetch_row($rs))
{
$a=odbc_result($rs,"RAL_APPCode");
$b=odbc_result($rs,"RAL_InstallationLink");
$link=odbc_result($rs,"RAL_ApplicationLink");
$d=odbc_result($rs,"RAL_APPIconLocation");
}
$linktoOpen = $link.$user.$appCode;
return trim($link);
odbc_close($actveConn);
}
}
?>
DatabaseConnection.php
<?php
class dbconnection{
public function dbOpen(){
$conn=odbc_connect("Global02","USER-00","USER00");
if (!$conn){
exit("Connection Failed: " . $conn);
}
else{
return $conn;
}
}
}
?>
when i convert it it always have an error.
Simple steps you have to follow, you can need to check user guide first, anyway try this
You have to follow these steps:
Step 1: For database connection
In database.php
set your database configuration inside $db['default']
Load your database using autoload.php
$autoload['libraries'] = array('database');
Step 2:
Create model Example Mdl_mis.php
Example:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Mdl_mis extends CI_Model {
function getAllApplink(){
$this->db->select("SELECT a.RAL_Code,a.RAL_App_Name,a.RAL_Remarks,b.RAL_APPIconLocation,b.RAL_ApplicationLink");
$this->db->from("R_Application_List as a");
$this->db->join("R_Application_Links as b","a.RAL_Code=b.RAL_APPCODE",'INNER');
$this->db->where("a.RSC_Active",1);
$result = $this->db->get()->result_array();
return $result;
}
function getAppLink($appCode,$user){
$this->db->select("*");
$this->db->from("R_Application_Links");
$this->db->where("RAL_APPCode",$appCode);
$result = $this->db->get()->row_array();
$a = $result['RAL_APPCode'];
$b = $result['RAL_InstallationLink'];
$link = $result['RAL_ApplicationLink'];
$d = $result['RAL_APPIconLocation'];
$linktoOpen = $link.$user.$appCode;
return trim($link);
}
}
?>
Step 3:
Create Controller example MisApplication.php
Create one method also
Example:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MisApplication extends CI_Controller {
public function index()
{
/* Load you model first */
$this->load->model('Mdl_mis');
$data['all_link'] = $this->Mdl_mis->getAllApplink();
/* Create one view called index.php and load it and pass your data*/
$this->load->view('index',$data);
}
}
?>
Step 4: Put code in index.php view
<!DOCTYPE html>
<html>
<body>
<table align=center cellpadding=10>";
<tr height=120>";
<?php
$appcount=0;
foreach($all_link as $rs))
{
$appcode = $rs["RAL_Code"];
$appname = $rs["RAL_App_Name"];
$desc = $rs["RAL_Remarks"];
$icon = $rs["RAL_APPIconLocation"];
$applink = trim($rs["RAL_ApplicationLink"]);
if($appcount==3){
echo "<tr height=120>";
//echo "<td>";
$appcount=0;
}
//echo "<td>".$appcode."</td>";
//echo "<td>".$appname."</td>";
echo "<td valign=bottom><img src=".$icon."height=30 width=150 onclick='openApp(\"$appcode\",\"$user\",\"$applink\");' </img></td>";
//echo "<td><input type=\"button\" ></td>"
//echo "<td>".$desc."</td>";
echo '<script>openApp();</script>';
$appcount++;
}
echo "</table>";
?>
<img src="dtr.png" height="30" width="150" onclick=openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $this->Mdl_mis->getAppLink("0001",$user); ?>") ></img></br>
<input type="button" id="btn.DTR"; name="btn.Dtr"; value="DTR"; onclick='openApp("0001","<?php echo $user; ?>","<?php echo $appLink = $this->Mdl_mis->getAppLink("0001",$user); ?>")'/>
<input type="button" id="jose"; name="btn.OpenApp"; value="Approval System(test)"; background="dtr.png";onclick='openApp("0014","<?php echo $user; ?>","<?php echo $appLink = $this->Mdl_mis->getAppLink("0002",$user); ?>")'/>
<script language="javascript">
function openApp(Appcode, User, link){
var OpenLink;
OpenLink = link+"?A1="+User+"&A2="+Appcode; // Concatenating strings
//alert(OpenLink);
//document.write (OpenLink); // printing the Concatenated string
//window.open(OpenLink);
//window.open(OpenLink,'_blank', 'toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,left=10000, top=10000, width=10, height=10, visible=none', '');
window.location.href = OpenLink;
}
</script>
</html>
I am currently having trouble with this. I would like to make one of my variables in Javascript have a PHP value. Here is what I mean:
<script>
JSvariable = <?php echo $PHPvariable; ?>;
</script>
For some reason that is not working. Here is my full (snippet) of code:
<script>
currentreplyid = <?php echo $allpostcomments[$key]['replyid']; ?>;
$('#parentcommentholder').val(currentreplyid);
</script>
I am sure it is some stupid mistake, but I can not seem to find it! What is the problem? Thank you!
PS #parentcommentholder is an input field, and it just had the value 0 after the field is supposed to of been changed.
Here is some source:
<?php
$postcommentsquery = "SELECT * FROM comments WHERE parent = :parent AND postid = :postid ORDER BY datecreated DESC";
$postcommentsparams = array(':parent' => $allreplies[$key]["postid"],
':postid' => $postid);
try{
$postcommentsstmt = $connection->prepare($postcommentsquery);
$postcommentsresult = $postcommentsstmt->execute($postcommentsparams);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$allpostcomments = $postcommentsstmt->fetchAll();
foreach ($allpostcomments as $key => $value) {
?>
<script>
var currentreplyid = <?php echo $allpostcomments[$key]['replyid']; ?>;
$('#parentcommentholder').val(currentreplyid);
</script>
<input id="parentcommentholder"></div>
Don't forgot for give quotes ' or ". Use following:
<script>
var JSvariable = '<?php echo $PHPvariable; ?>';
//or
var JSvariable = "<?php echo $PHPvariable; ?>";
</script>
Reason: If php variable contains string and if while assigning it to javascript variable we shall not give quote like:
<?php $PHPvariable = 'String';?>
var JSvariable = <?php echo $PHPvariable; ?>;
Will transform into :
var JSvariable = String;//which will give error in javascript
But this will work fine if PHP variable contains a numeric value like:
<?php $PHPvariable = 2;?>
var JSvariable = <?php echo $PHPvariable; ?>;
Will transform into :
var JSvariable = 2;//which will work perfect
Complete code should be:
<script>
var currentreplyid = "<?php echo $allpostcomments[$key]['replyid']; ?>";
//or if you are sure your variable contains int value
var currentreplyid = parseInt("<?php echo $allpostcomments[$key]['replyid']; ?>");
$('#parentcommentholder').val(currentreplyid);
</script>
Try the below instead of using javascript (as I don't think you need it):
<?php
$postcommentsquery = "SELECT * FROM comments WHERE parent = :parent AND postid = :postid ORDER BY datecreated DESC";
$postcommentsparams = array(':parent' => $allreplies[$key]["postid"],
':postid' => $postid);
try{
$postcommentsstmt = $connection->prepare($postcommentsquery);
$postcommentsresult = $postcommentsstmt->execute($postcommentsparams);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$allpostcomments = $postcommentsstmt->fetchAll();
foreach ($allpostcomments as $key => $value) {
?>
<input id="parentcommentholder" value="<?php echo ((int)$allpostcomments[$key]['replyid']>0) ? $allpostcomments[$key]['replyid'] : 0; ?>" />
<?php
}
?>
If your defiantly sure $allpostcomments[$key]['replyid'] is bringing back a value, this should work without any issues.