[object HTMLTablerowElement] - javascript

I have a problem, trying to create a button with Ajax/Php which is to appear on my main page and is supposed to have a onclick function. To be more specific, I end up in a while loop, which fetches through the rows of my mySQL-table and displays the following:
echo "<table>";
while($result == true && $a <= 15){
$res = $result["uberschrift"];
echo "<tr id='$res'>";
echo "<td>".$a."</td>";
echo "<td>".$res."</td>";
echo "<td>"."<button id='b_löschen' onclick='k_löschen($res)'> löschen </button>"."</td>";
echo "</tr>";
$result = $stmt->fetch();
$a++;
}
echo "</table>";
In fact, this works quite well, and on my main page the string contained in $result["uberschrift"] is displayed properly. However, if I call the function k_löschen with $res as shown above, it will not take the String but a certain
[object HTMLTablerowElement] as input, which naturally causes an Error. Can someone tell me how to change the code so that the function actually takes the string as it's input?

You have issues with your quotes. Also you could tell us what $res typically contained.
I would do this
echo "<tr id=\"".$res."\">";
echo "<td><button id='b_löschen' onclick='k_löschen(\"".$res."\")'>löschen</button></td>";

Related

How can I perform math when jquery class equals a certain value?

I'm trying to manipulate an amount based on the name of class that changes with jQuery. My amount comes from here:
<script>
var inboundAmount = <?php echo json_encode($inbound); ?>;
</script>
Let's just say that amount is 10.
I output with a bit of php and script like this:
echo "$<script>document.write(inboundAmount);</script>";
The class changes when a user clicks on it. Sample classes are as follows:
half first3547
pending first3547
paid first3547
What I've been trying to do is make it so that if the class contains the word 'half', then it would do some math:
document.write(inboundAmount/2);
Producing the number 5.
I've tried this and some variations with no luck:
if ($("#mycontent").find("a.half").length > 0) {document.write(inboundAmount/2);}
'mycontent' is the id for the table that a href exists in.
I have also tried the following:
if ( $( this ).hasClass( 'half' ) ) { {document.write(inboundAmount/2);}
still to no avail.
A full example is this:
<td class='grav-results-td'>
<?php if (!empty($check_inbound)):echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound',
$vendor)\">$<script>if ( $( this ).hasClass( 'half' ) )
{document.write(inboundAmount/2);}</script></a>"; endif; ?></td>
And I have also tried the manipulation in the success area of my ajax script.
Answering some of the questions below:
$inbound_status first$id is a PHP variable.
The output of document.write(inboundAmount/2); is working correctly and showing half the amount of document.write(inboundAmount);
The this scope inside your <script> tag will not point to the element you expect. Instead of checking in javascript you can check the condition in php itself. Otherwise, you can refer the code below to do it on javascript. You can use the php variable to check whether it contains the string half
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">
$<script>if ('$inbound_status' == 'half' )
{document.write(inboundAmount/2);}
</script>
</a>";
endif;
echo "</td>";
If inboundAmount is a php variable you can do it easily without using javascript
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">$";
if ($inbound_status == 'half'):
echo $inboundAmount/2;
endif;
echo "</a>";
endif;
echo "</td>"

extract data from unformatted CSV file

I want to display only certain parts from some.csv on webpage.
I used following code to display the whole row but it doesn't look good.
some.csv
"1","[('1012', 5), ('1046', 5), ('1052', 5), ('1056', 4.9910199299984184), ('1030', 4.8534167927670344), ('1040', 4.8212814442619543), ('1035', 4.7661155679960796), ('1034', 4.6870279945429703), ('1042', 4.5541209598953358), ('1027', 4.5151900893812948)]"
"2","[('1049', 5), ('1016', 4.6133266327772251), ('1010', 4.5357481760897569), ('1046', 4.3349671883556038), ('1050', 4.3227509505599846), ('1037', 4.2902806273386966), ('1041', 4.2507193844291828), ('1053', 4.0984615265310147), ('1009', 4.098439317076469), ('1030', 4.0)]"
"3","[('1012', 4.9811840834706604), ('1016', 4.8312378650104533), ('1041', 4.7528923569042405), ('1049', 4.5049427949269853), ('1050', 4.3853256507131384), ('1045', 4.1598878939000565), ('1005', 4.1014830595313585), ('1022', 4.0958233778390403), ('1054', 4.0854105427315339), ('1024', 4.061922071985868)]"
code.php
<?php
$f = fopen("C://xampp/htdocs/Game/python/some.csv", "r");
$i = 0;
while (($line = fgetcsv($f)) !== false) {
$i++;
echo "<tr>";
foreach ($line as $cell) {
if($i==1)
echo "<td>" . htmlspecialchars($cell) . "</td>";
}
echo "</tr>\n";
}
fclose($f);
?>
what I want is the output should look like this:
1012
1046
1052
1056
1030
1040
1035
1034
1042
1027
The problem is, the second element in the array produced by fgetcsv looks like Python code. You can extract the first element from each of those tuples with a regex.
while (($line = fgetcsv($f)) !== false) {
preg_match_all("/'(\d+)'/", $line[1], $matches);
echo '<tr>';
foreach ($matches[1] as $number) {
echo "<td>$number</td>";
}
echo '</tr>';
}
If the CSV is produced by a Python script, it would be better to modify that script so that it only outputs the numeric values you need in the CSV file, or outputs the results in a format that PHP can handle directly, like JSON.
I'm not sure exactly how you want to format the result as a table, so I'm kind of guessing at that part, but you should be able to do whatever you need with it once you get those numbers in an array.

Access to a PHP array element from Javascript function

I am trying to set some fields of a form with the values of an array.
I have an array like this:
Array
{
'elementID1' => Array
{
'id1' => 'some value',
'id2' => 'some value',
'id3' => 'some value',
...
}
'elementID2' => Array
{
'id1' => 'some value',
'id2' => 'some value',
'id3' => 'some value',
...
}
...
}
The array is filled with the response of a webservice to whom I sent some parameters with a form. Once I filled the array, I draw a table and iterate over the array.
<table width = "100%" border = "1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>View details</th>
</tr>
<?php
foreach($array as $id => $detail)
{
echo "<tr>";
echo "<td>";
if(array_key_exists('id1', $detail))
echo $detail['id1'];
echo "</td>";
echo "<td>";
if(array_key_exists('id2', $detail))
echo $detail['id2'];
echo "</td>";
echo "<td>";
if(array_key_exists('id3', $detail))
echo $detail['id3'];
echo "</td>";
echo "<td>";
echo "View more";
echo "</td>";
echo "</tr>";
}
?>
</table>
Now, here comes the problem, as you can see there is a fourth column called View details, this column is filled with a hyperlink that calls a javascript function with the elementID value of the specific row as parameter.
A little more below I have the script:
<script>
function equisYe(id)
{
var equis = "<?php echo $array['" + id + "']['IncidentID']; ?>";
alert(equis);
}
</script>
When I click on the hyperlink doen't happen anything, I think the problem has something to do with the special characters inside the quote punctuations. In my browser, when I inspect one of the hyperlinks, it shows the following message inside de scripts tags:
function equisYe(id)
{
var equis = "<br />
<b>Notice</b>: Undefined index: + id + in <b>
";
alert(equis);
}
That means, for some reason, the strings aren't concatenating, and if I delete a 'p' from php tag in the equis var like this:
var equis = "<?ph echo $array['" + id + "']['IncidentID']; ?>";
Then the alert shows this:
"<?ph echo $array['10']['IncidentID']; ?>"
So, it works when it doesn't have the php tags.
The other answer is correct, but not explaining it at all (or even politely). The PHP stuff runs before your Javascript stuff does. You'll never get this to work this way, unfortunately. If it was the other way around, it might work, but this isn't going to work the way you have it.
The only way to have something like this working is to have the ID value chosen in advance and have PHP set something in Javascript. Javascript can never set a PHP value simply by virtue of PHP being run prior to the page being served and Javascript being run after the page is served.
dude... javascript executes on client(browser) and php on server; you CANNOT do that
try to use <? ?> instead of <?php ?>
Everyone is correct in that you can't simply call PHP functions from Javascript without performing some kind of HTTP request.
Since you already have the "IncidentID" in php while you're generating the page, you can simply write it to the page.
instead of
echo "<td>";
echo "View more";
echo "</td>";
do
echo "<td>";
echo "View more";
echo "</td>";
You can use data attribute to pass the $id to js, always using the DOM.
Check this: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes
Then use a click event in "view details" to get the data from element and trigger the function, this way you will be abble to work with it

Array value is replaced when second one stores in php

I am storing session values into array getting from URL and try to display it (getting size of product in a shopping cart).
But first value is replaced by second one.
My code:
if(isset($_POST['radio'])){
$_SESSION['sz']=$_POST['radio'];
$si=$_SESSION['sz'];
}
<a href="product_detail.php?pdt_id='.$pdid.'&add=' .$pdid .'&size='.$si.'"
class="cartBtn" onclick="return fun1()">Add to cart</a>';
?>
Display page:
$rt=$_GET['size'];
$_SESSION['wer']=$rt;
$array = $_SESSION['wer']; //Assigns session var to $array
//print_r($array);
echo $array[$x];
}
About First part mistakes :
<?php
if(isset($_POST['radio'])){
$_SESSION['sz']=$_POST['radio'];
$si=$_SESSION['sz'];
}
Add to cart';
?>
Your a tag is completely wrong as #Hearner said. It should be out of the php tag or inside with an "echo" like this :
echo "<a href='product_detail.php?pdt_id=".$pdid."&add=".$pdid."&size=".$si."' class='cartBtn' onclick='return fun1()'>Add to cart</a>";
You cannot access your $si variable out of your if statement. As written here over, if your $si isn't declared before (since you said that it was not your complete code...) then $si (in the link href) does not exist. You should therefore declare it before your if statement OR place your link (a tag) inside your if statement too!
What if your "$_POST['radio']" is NOT set ?? what happens? code missing... !! is $si declared anyway?
About Second part :
<?php
for( $x = 0, $max = count($array); $x < $max; ++$x ) {
$rt=$_GET['size'];
$_SESSION['wer']=$rt;
$array = $_SESSION['wer'];
echo $array[$x];
}
?>
I don't understand what you're trying to do here... need more code/information... cannot help more without your whole code...
EDIT :
Here is a very simple example to show you how to keep your get vars into a session array.
page one (pageOne.php):
<?php
session_start();
if(!(isset($_SESSION['myTest']))){
$_SESSION['myTest'] = "AWESOME";
$_SESSION['varToKeep'] = [];
}else{
echo "A session is already started. This is : ".$_SESSION['myTest']."<br/>";
if(count($_SESSION['varToKeep']>0)){
echo "There are ".count($_SESSION['varToKeep'])." vars in the array!<br/>";
for($i=0;$i<count($_SESSION['varToKeep']);$i++){
echo "Item ".$i." : ".$_SESSION['varToKeep'][$i]."<br/>";
}
}
}
echo "<br/>Click below to add a value in array<br/>";
$random = rand(1,100);
echo "<a href='pageTwo.php?mygetvar=STACKTEST".$random."'>Click here</a>";
?>
page two (pageTwo.php):
<?php
session_start();
echo "myTest value is : ".$_SESSION['myTest']."<br/><br/>";
$value = $_GET['mygetvar'];
$_SESSION['varToKeep'][] = $value;
echo "<a href='pageOne.php'>CLICK HERE TO RETURN ON PAGE ONE!</a>";
?>

How to retrieve $_SESSION value in a JavaScript/HTML

I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.

Categories

Resources