I have one function and my echo is:
echo '<tr>
<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley("hallo")"> <br>:illuminati:</td>
</tr>'
The problem is that onclick="insertSmiley("hallo")" must be with ' and not with ". If I put this in html everything works, but in nothing happens in php echo when I click.
My index.php have this script in body:
<script type="text/javascript">
function insertSmiley(smiley)
{
var currentText = document.getElementById("send");
var smileyWithPadding = " " + smiley + " ";
currentText.value += smileyWithPadding;
currentText.focus();
}
</script>
and the other code is in my chat.php:
echo '<textarea id="send" maxlength="125" rows="2" placeholder="Enter your message"></textarea>
<tr>
<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley("hallo")"> <br>:illuminati:</td>
</tr>
I really think that the problem is because I cant use '' in echo and I need it for onClick...('hallo').
use \' to just print '
Backslashes are used in PHP to escape special characters within quotes. As PHP does not distinguish between strings and characters
If your write like this
echo 'check it \' out';
it will give output like this
check it ' out
So use like this
echo '<tr>
<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley(\'hallo\')"> <br>:illuminati:</td>
</tr>'
The fact that you are not echoing any variable within the echo statement, simply add the PHP tags outside of your code:
//Your PHP code here
?>
<tr>
<td align="center" style="padding:5px;">
<img src="/chat/emotes/smile.png" onclick='insertSmiley(" hallo")'>
<br>:illuminati:
</td>
</tr>
<?php
// Continue your php code
In case you have to echo a variable in your HTML, do it this way:
$greetings = "Hallo";
//Your PHP code here
?>
<tr>
<td align="center" style="padding:5px;">
<img src="/chat/emotes/smile.png" onclick="insertSmiley('<?php echo $greetings; ?>')">
<br>:illuminati:
</td>
</tr>
<?php
Try this:
<?php
echo "<textarea id="send" maxlength="125" rows="2" placeholder="Enter your message"></textarea>";
echo "<tr>";
echo "<td align="center" style="padding:5px;"><img src="/chat/emotes/smile.png" onclick="insertSmiley("hallo")"> <br>:illuminati:</td>";
echo "</tr>";
?>
You can use:
onclick="insertSmiley(\'hallo\')"
Related
I am running this code in a PHP file
<?php
if(empty($item)){
echo "Your cart is empty";
}
else{
echo '<table id="cart_table"><tr><th>Item</th><th>Price</th><th></th></tr>' ;
for($i = 0;$i< count($item);$i++){
if(!empty($item[$i])){
$numRow++;
echo '<tr><td>' . "<img src = 'images/$photo[$i].jpg'>" .'</td>';
echo '<td>' .'$' . $item[$i] . '</td>';
echo '<td>' . '<button id= "btn" onclick = "function(){
<script type="text/javascript">
document.getElementById("cart_table").deleteRow('. $numRow .');
}"> Remove</button>'.'</tr>' ;
}
}
echo '</table>';
}
?>
Please consider these lines of code only from the above snippet:
echo '<td>' . '<button id= "btn" onclick = "function(){
<script type="text/javascript">
document.getElementById("cart_table").deleteRow('. $numRow .');
}"> Remove</button>'.'</tr>' ;
}
The result of this code is:
The result I am trying to achieve is:
Is it possible to achieve this result? The result is going to be a button with the label "Remove" and with the onClick property that calls a INLINE(I know it's bad practice, but I have a good reason for it) function which executes some lines of code.
Try not to mix up HTML and PHP together, if there is a need to mix HTML and PHP, try in proper formatting.
<?php
if(empty($item)):
?>
<span>Your cart is empty</span>
<?php
else:
?>
<table id="cart_table">
<tr>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
<?php
for($i = 0;$i< count($item);$i++):
if(!empty($item[$i])):
$numRow++;
?>
<tr>
<td><img src ='images/<?php echo $photo[$i].jpg; ?>'/></td>
<td>$<?php echo $item[$i]; ?></td>
<td><button id= "btn" onclick = "deleteFromCart('<?php echo $numRow; ?>')"> Remove</button></td>
</tr>
<?php
endif;
endfor;
?>
</table>
<?php
endif;
?>
<script>
function deleteFromCart(rowToDelete){
document.getElementById("cart_table").deleteRow(rowToDelete);
}
</script>
I'm having some issues with dynamic tables and js code
Here's the table
<table class="table table-bordered table-striped responsive-utilities" id="m1r2">
<thead>
<tr>
<th data-toggle="tooltip" title=" <?php echo $lang['m1r7AuditoresPlace'] ; ?>"> <?php echo $lang['m1r7Auditores']; ?> </th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" data-toggle="tooltip" title=" <?php echo $lang['m1r7AuditoresPlace'] ; ?>">
<select class="select2 m-b-10 select2-multiple" multiple="multiple" data-placeholder="Seleccione" name="m1r7Auditores1[]">
<?php foreach ($auditores as $aud ): ?>
<option value="<?php echo $aud->userRut; ?>"><?php echo ''.$aud->userRut.'</option>
<?php endforeach; ?>
</select> -
</th>
</tr>
</tbody>
</table>
And the JS to add more rows is like this
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = document.getElementById('cuantos').value;
var num2 = 1;
var sum = Number(num) + num2;
document.getElementById("m1r2").insertRow(-1).innerHTML = '<tr>' +
'<th scope="row" data-toggle="tooltip" title=" <?php echo $lang['m1r7AuditoresPlace'] ; ?>"><select multiple="multiple" name="m1r7Auditores1[]">' +
' <?php foreach ($auditores as $aud ): ?>' +
' <option value="<?php echo $aud->userRut; ?>"><?php echo ''.$aud->userRut.'</option>' +
' <?php endforeach; ?>' +
'</select></th>' +
'</tr>';
The result its the first one working like a charm, but then, when I add some rows they came plain (no style, no js) as image shown
Thanks in advance for the help :)!
problem solved! if anyone got the same issue, the js DOM wasnt re declared afte the tr was duplicated, so I just added
$('#select'+ sum +'').multiSelect();
at the end of the code shown above :)
Hello devs/programmers/coders.. When I press Tab to proceed to the next row NaN value is automatically inputted, and even onclick gives me NaN, then the sum of the total column will NaN and will not change.. I have no idea on how will I remove the NaN value... please help me! Here is my code:
<html>
<link rel = "stylesheet" href = "styling.css?version=3"></link>
<body>
<div id='acctdetails' style='padding-left: 15% '>
<?php
echo "USERNAME: ". $_SESSION["dynau"];
?>
OR Date: <input type='text' placeholder='OR Date' name='ordate' value='<?php echo date('M, d, Y');?>'>
OR ID: <input type='text' placeholder='OR ID' name='orid'>
Payor : <input type='text' placeholder='Payor' name='payor' >
</div>
<center>
<form method="POST">
<?php
$result = mysqli_query($link,"SELECT * FROM account_db");
echo"<html>";
echo "<script>";
echo "function calculate(amount, id){
document.getElementById('total' + id).innerHTML = parseInt(document.getElementById('copy' + id).value) * amount;
var x=parseInt(document.getElementById('total' + id).innerHTML);
var y = document.getElementById('sumtotal').innerHTML;
var a = y.split(' ');
var z = parseInt(a[1]) + parseInt(x);
document.getElementById('sumtotal').innerHTML = 'Total: ' + z;
}";
echo "</script>";
echo"<center>";
echo "<form method='POST'>";
echo "<br></br>";
echo "<table style='border:1px solid black' id='thetable' name='pleasework'>";
echo"<th>FILES</th>";
echo"<th>AMOUNT</th>";
echo"<th>NO. OF COPIES</th>";
echo"<th>TOTAL AMOUNT</th>";
$counter = 0;
while($row = mysqli_fetch_row($result))
{
echo"<tr>";
echo "<td >$row[1] </td>";
echo "<td align=center>$row[2] </td>";
echo "<td align=center>
<input type='number' id='copy" . $counter . "' onkeyup='calculate(" . $row[2] . ", " . $counter . ")'>
</td>";
echo "<td align=center id='total" . $counter . "'></td>";
echo"</tr>";
$counter++;
}
echo"<tr>
<td id='sumtotal'>TOTAL: 0</td>
</tr>";
echo "</table>";
echo " <br><div style='padding-left: 15px'><input type='submit' id='btn1' value='Transact' name='transaction' onclick='javascript: window.print()'></div>";
echo"</center>";
echo"</html>";
?>
</center>
</body>
</html>
Your first issue is in this line:
document.getElementById('total' + id).innerHTML = parseInt(document.getElementById('copy' + id).value) * amount;
In order to understand the issue you can try by yourself:
parseInt('')
The result of the previous operation is undefined, so:
parseInt('')+amount
will be NaN (not a number).
That means you need to test the value before to proceed with the computation:
!!ele.value.trim() --> value is not empty
I suggest you to use:
.value (for input field): The initial value of the control. This attribute is optional except when the value of the type attribute is radio or checkbox.
Note that when reloading the page, Gecko and IE will ignore the value specified in the HTML source, if the value was changed before the reload.
.textContent(for cell with text values): represents the text content of a node and its descendants
instead of:
.innerHTML : sets or gets the HTML syntax describing the element's descendants.
Moreover, in order to simplify your code you may consider to traverse the dom in order to get the values contained in the other two sibling cells.
Use the keyword this in your case. This keyword stands for the current element, so you don't need to get it from the dom.
The snippet:
function calculate(ele) {
if (!!ele.value.trim()) {
var x = +ele.value * +ele.parentElement.previousElementSibling.textContent.trim();
var y = +document.getElementById('sumtotal').textContent.trim().split(' ').pop();
ele.parentElement.nextElementSibling = x;
var z = x + y;
document.getElementById('sumtotal').textContent = 'Total: ' + z;
}
}
<div id='acctdetails' style='padding-left: 15% '>
USERNAME: ...
OR Date: <input type='text' placeholder='OR Date' name='ordate' value='01/01/2017'>
OR ID: <input type='text' placeholder='OR ID' name='orid'>
Payor : <input type='text' placeholder='Payor' name='payor'>
</div>
<form method="POST"><br/>
<table style='border:1px solid black' id='thetable' name='pleasework'>
<th>FILES</th>
<th>AMOUNT</th>
<th>NO. OF COPIES</th>
<th>TOTAL AMOUNT</th>
<tr>
<td>1</td>
<td align=center>11</td>
<td align=center>
<input type='number' id='copy1' onkeyup="calculate(this)">
</td>
<td align=center id='total1'></td>
</tr>
<tr>
<td id='sumtotal'>TOTAL: 0</td>
</tr>
</table>
<br>
<div style='padding-left: 15px'><input type='submit' id='btn1' value='Transact' name='transaction'
onclick='javascript: window.print()'></div>
</form>
I have assigned three select options with the same name which will be stored in the my database table. My code was working well at first right now i don't why it's working well. right now it only saves the value assigned to the last select option panel. Please i need help
<?php
if(isset($_POST['submit'])){
$vic_title = $_POST['vic_title'];
$vic_name = $_POST['vic_name'];
echo $vic_name;
if($vic_name=='')
echo "<font color='Green'><b>Please fill in the discription the accused name THANKS!!</b></font>";
else
$insert = "INSERT INTO discips(vic_title, vic_name)
values('$vic_title','$vic_name')";
$run = mysql_query($insert);
if ($run) {
echo "<font color='Green'><b>the incident was added</b></font>";
# code...
}
else{
echo "<font color='red'><b>the incident was not added</b></font>";
}
}
?>
Here is my form that i used.
<form name="harvets" id="form" action="?pg=<?php echo $_GET[pg]."&lodge_inc"."&hv=$token"; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $edit_ca;?>">
<center style="padding-top: 2%; margin-top: 3%;"><h3>Enter Incident Informtion</h3></center>
<table width="100%" class="m_aligned">
<tr>
<td align="right" style="width: 100%;">Victim *</td>
<td align="left" style="width: 100%;">
<select style="width: 100%;" id="victim" name="vic_title" class="sect" placeholder="Select a Role">
<option></option>
<option value="staff">Staff</option>
<option value="student">Student</option>
<option value="visitor">Vistor</option>
</select>
</td>
</tr>
<tr id="staff_name" style="display: none;">
<td align="right" style="width: 100%;">Staff Name : </td>
<td align="left" style="width: 100%;">
<select style="width: 100%;" name="vic_name" class="sect" placeholder="Staff's Name">
<?php
$get_staf = "select * from useraccounts";
$run_staf = mysql_query($get_staf);
while ($row = mysql_fetch_array($run_staf)) {
$staf_id = $row['username'];
$staf_name = $row['name'];
echo "<option value='$staf_id'>". $staf_name ."</option>";
# code...
}
?>
</select>
</td>
</tr>
<tr id="vis_name" style="display: none;">
<td align="right" style="width: 100%;">Visitor Name : </td>
<td align="left" style="width: 100%;"><input type="text" name="vic_name" placeholder="Visitor's Name"></td>
</tr>
<tr id="stud_name" style="display: none;">
<td align="right" style="width: 100%;">Student Name: </td>
<td align="left" style="width: 100%;">
<select style="width: 100%;" class="sect" name="vic_name" placeholder="Student's Name" cols="9">
<option></option>
<?php
$get_stud= "SELECT * FROM studentdetails";
$run_stud = mysql_query($get_stud);
while ($row = mysql_fetch_array($run_stud)) {
$stud_id = $row['id'];
$stud_fname = $row['fname'];
$stud_lname = $row['lname'];
echo "<option value='$stud_id'>". $stud_fname ." ". $stud_lname ."</option>";
# code...
} ?>
</select>
</td>
</tr>
SAVE
Here is My JavaScript
<script type="text/javascript">
$("#victim").change(function (ev){
if($(this).val()=='visitor') $("#vis_name").css("display", "table-row")
else $("#vis_name").css("display", "none")
if($(this).val()=='student') $("#stud_name").css("display", "table-row")
else $("#stud_name").css("display", "none")
if($(this).val()=='staff') $("#staff_name").css("display", "table-row")
else $("#staff_name").css("display", "none")
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$(".sect").select2({
allowClear: true
});
</script>
Getting the value of the last field (select or anything) using a given name is the correct behaviour. If you wish to send multiple values when submiting the form, you must give different names to their fields.
Ask yourself why you want to name them the same way. How are you supposed to get them ? If I create three different inputs, name the three of them 'title' and submit the form after type different things in each input, what do you guess I'll get if I access $_POST['title'] ? More problematic, what should I type to get the value of the first of my three inputs ? How the hell would I know, these are identical elements with different purposes !
If you design different elements, give them different features or they won't be different. They will just overwrite each other and you'll only have the last of the lot.
If you truly need to have them named the same, add hooks at the end of the name like this :
name="vic_name[].
It will append the value of that field to $_POST['vic_name'], which will now be an array, and therefore may contain multiple values. That's the only way.
I have solved the problem. I created two files by using AJAX to call another file to replace one a certain line of code. Sometimes we may want something and we fail in someway or another, but when we focus deeply we can solve the code.
i replaced my Javasrcipt file with
<script type="text/javascript">
$("#victim").change(function () {
var cat = $(this).val();
$.ajax({
type: "GET"
, url: "student/fetch_victim.php"
, data: "n=" + Math.random() + "&vr=" + cat
, beforeSend: function () {
$("#ctg").html("<img src='imgs/loader.gif' />...loading")
}
, success: function (response) {
$("#tryme").html(response)
}
});
});
</script>
and i moved the sections i wanted to another file
<?php
require "../ht.php"; $hom = new ht;
if($_GET['vr']){
$q = $_GET['vr'];
if($q =='staff'){
echo "
<td align='right' style='width: 100%;'>Staff Name : </td>
<td align='left' style='width: 100%;'>
<select name='vic_name' class='sect' style='width: 100%;' value='<?php echo $edit[2] ?>' placeholder='Staff's Name'>";
$staf = mysql_query("SELECT * FROM useraccounts"); $w=mysql_error();
while ($row = mysql_fetch_array($staf)) {
echo "<option value='$row[0]'>". $row[1] ."</option>";
# code...
}
echo "</select>
</td>
";
}elseif ($q == 'student') {
echo "
<td align='right' style='width: 100%;'>Student Name: </td>
<td align='left' style='width: 100%;'>
<select style='width: 100%;' class='sect' name='vic_name' value='".$edit[2] ."' placeholder='Student's Name' cols='9'>
<option></option>";
$stud= mysql_query("SELECT * FROM studentdetails");
while ($row = mysql_fetch_array($stud)) {
echo "<option value='$row[31]'>". $row[0] .' '. $row[1] ."</option>";
# code...
}
echo "</select>
</td>
";
}else{
echo "
<td align='right' style='width: 100%;'>Visitor Name : </td>
<td align='left' style='width: 100%;'><input style='width: 100%;' type='text' name='vic_name' value='".$edit[2] ."'placeholder='Visitor's Name'></td>
";
}
}
?>
<script type="text/javascript">(function($){
var code = $('.html-container').html();
$('.html-viewer').text(code);
})(jQuery);</script>
I'm retrieving data from mysql like this structure =>
echo "<table id='postI" . $chat_row['id'] . "'>";
echo "<tr>";
echo "<td><p class='post_author'>" . $chat_row['user_name'] . "</p><a href='javascript: void(0)' class='edit_post_a'><div class='edit_post'></div></a></td>";
echo "</tr>";
echo "<tr>";
echo "<td><p class='posted_on'>" . $chat_row['posted_on'] . "</p></td>";
echo "</tr>";
echo "<tr>";
echo "<td><br></td>";
echo "</tr>";
echo "<tr>";
echo "<td><p class='posted_msg'>" . $chat_row['message'] . "</p></td>";
echo "</tr>";
echo "</table>";
and I can get the table id with this expression =>
$(".edit_post_a").bind("click",function(){
var tb_id = $(this).closest("table").attr("id"); // works , gets the id of the table
alert($(tb_id + ".posted_msg").text()); // don't work, gets nothing , just alert box
});
My problem is that want to know text of the third p, what I've tried =>
alert($("#" +tb_id + ".posted_msg").text());
alert($("#" +tb_id).find("p").last().text());
alert($("#" +tb_id + " p:nth-child(3)").text());
nothing works , get empty alert box , I've no idea whats wrong ? how can I fix it ? thanks
HTML code =>
<table id='postI245'>
<tr>
<td><p class='post_author'>USER</p><a href='javascript: void(0)' class='edit_post_a'><div class='edit_post'></div></a>
</td>
</tr>
<tr>
<td><p class='posted_on'>DATE</p></td>
</tr>
<tr>
<td><br></td>
</tr>
<tr>
<td><p class='posted_msg'>MSG</p></td>
</tr>
</table>
and so on ...
Your selector selects nothing, you should add # for selecting an element by id.
var txt = $('#'+tb_id + " .posted_msg").text();
or:
var txt = $(this).closest("table").find("p:eq(2)").text()