Modal doesn't open when php variables value is large - javascript

I am using an edit button inside while loop for each row of table to pass a row values into a modal form.
<td><a class="custom-links" onclick='EditModal("<?php echo $data['id']; ?>","<?php echo $data['name']; ?>","<?php echo $data['price']; ?>","<?php echo $data['description']; ?>","<?php echo $data['type']; ?>","<?php echo $data['cooking_instructions']; ?>","<?php echo $data['ingredients']; ?>","<?php echo $data['allergen_warnings']; ?>","<?php echo $data['storage_instructions']; ?>","<?php echo $data['case_size']; ?>","<?php echo 'uploaded_images/'.$data['image']; ?>")'>
<button type="button" id="<?php echo $data['id']; ?>" class='btn btn-primary glyphicon glyphicon-edit'></button></a></td>
Then i set values like
function EditModal(id,name,price,description,type,cooking_instructions,ingredients,allergen_warnings,storage_instructions,case_size,image){
document.getElementById("update_id").value = id;
document.getElementById("update_name").value = name;
document.getElementById("update_price").value = price;
document.getElementById("update_description").value = description;
document.getElementById("update_type").value = type;
document.getElementById("update_cooking_instructions").value = cooking_instructions;
document.getElementById("update_ingredients").value = ingredients;
document.getElementById("update_allergen_warnings").value = allergen_warnings;
document.getElementById("update_storage_instructions").value = storage_instructions;
document.getElementById("update_case_size").value = case_size;
document.getElementById("update_image_to_upload").src = image;
$('#update_menu_modal').modal('show');}
update_cooking_instructions, update_ingredients, update_allergen_warnings are textareas.
The problem is that modal gets opened when $data['cooking_instructions'], $data['ingredients'], $data['allergen_warnings'] contain less words or I remove them from EditModal function and when their values are large modal does not open.
Can anyone point out my mistake? What am I doing wrong? Modal is opening when I remove these three variables from EditModal

Give this a try, though note that you'll still have a problem with single quotes:
<td><a class="custom-links" onclick='EditModal(<?php echo json_encode($data['id']); ?>,<?php echo json_encode($data['name']); ?>,<?php echo json_encode($data['price']); ?>,<?php echo json_encode($data['description']); ?>,<?php echo json_encode($data['type']); ?>,<?php echo json_encode($data['cooking_instructions']); ?>,<?php echo json_encode($data['ingredients']); ?>,<?php echo json_encode($data['allergen_warnings']); ?>,<?php echo json_encode($data['storage_instructions']); ?>,<?php echo json_encode($data['case_size']); ?>,<?php echo json_encode('uploaded_images/'.$data['image']); ?>)'>
<button type="button" id=<?php echo json_encode($data['id']); ?> class='btn btn-primary glyphicon glyphicon-edit'></button></a></td>
I've removed the double quotes and instead used json_encode to emit a value. (I'm making the assumption that each value is a string on the PHP side.)
My guess as to the issue (though I'm stumped as to why it doesn't cause a console error) is that your "large" values have quotes or newlines in them. This sort of thing:
EditModal("<?php echo foo; ?>");
will turn into this if you have a newline:
EditModal("first line
second line");
which is not valid JavaScript. Similarly, a double quote would do this:
EditModal("Here is a quote --> " <-- see?");
which is also not valid JavaScript.
Using this instead:
EditModal(<?php echo json_encode(foo); ?>);
will turn those examples into these:
EditModal("first line\nsecond line");
and
EditModal("Here is a quote --> \" <-- see?");
both of which are fine.

Related

PHP/HTML Add onclick function to subcategories with certain name

I am trying to add an onclick function for only certain recursively generated subcategories.
This code currently displays the subcategories correctly:
<?php if($parent) { ?>
<?php $search_params['sCategory'] = $parent['pk_i_id']; ?>
<a href="<?php echo osc_search_url($search_params); ?>" class="parent active" data-name="sCategory" data-val="<?php echo $parent['pk_i_id']; ?>">
<span class="name"><?php echo $parent['s_name']; ?></span><em>(<?php echo ($parent['i_num_items'] == '' ? 0 : $parent['i_num_items']); ?>)</em>
</a>
<?php } ?>
<?php foreach($categories as $c) { ?>
<?php $search_params['sCategory'] = $c['pk_i_id']; ?>
<a href="<?php echo osc_search_url($search_params); ?>" class="child<?php if($c['pk_i_id'] == $search_cat_id) { ?> active<?php } ?>" data-name="sCategory" data-val="<?php echo $c['pk_i_id']; $
<span class="name"><?php echo $c['s_name'];?></span><em>(<?php echo ($c['i_num_items'] == '' ? 0 : $c['i_num_items']); ?>)</em>
</a>
<?php } ?>
</div>
I would like to call say functionX(); written in JS for only the subcategory named "catx".
FunctionX() is working correctly when called else where.
I've tried something like this but get a White screen:
<?php if($search_params == "catX") {
echo '<a onclick="functionX();" href="<?php echo osc_search_url($search_params); ?>" class="parent active" data-name="sCategory" data-val="<?php echo $parent['pk_i_id']; ?>">
<span class="name"><?php echo $parent['s_name']; ?></span><em>(<?php echo ($parent['i_num_items'] == '' ? 0 : $parent['i_num_items']); ?>)</em>
</a>;'
} else {
echo '<a href="<?php echo osc_search_url($search_params); ?>" class="parent active" data-name="sCategory" data-val="<?php echo $parent['pk_i_id']; ?>">
<span class="name"><?php echo $parent['s_name']; ?></span><em>(<?php echo ($parent['i_num_items'] == '' ? 0 : $parent['i_num_items']); ?>)</em>
</a>;' ?>
Pretty new to this, thanks for the help!
When you use an echo you are using ' to specify the borders of what you are going to output.
In your code you have echo 'Some content' but your content contains an ' aswell. Named: $parent['pk_i_id']
There a several ways to do this. Here is an example (Concatenation):
echo '<a onclick="functionX();" href="' . osc_search_url($search_params) . '">Click me</a>'; // ...
The inline notation is useful, in case you have surrounding HTML-Code:
<b>My name is<?php echo $name ?></b>
Just dont mix em :)
It's not an approach that works for everyone but in this kind of scenario I find it visually helpful (and very useful if I need to debug anything) to incrementally build a string using
statements
variables; and
conditional statements
and then echo the string, once I have built it.
Example:
<?php
$Item_Number = ($parent['i_num_items'] === '') ? 0 : $parent['i_num_items'];
$Link = '';
$Link .= '<a ';
if ($search_params === 'catX') {
$Link .= 'onclick="functionX();" ';
}
$Link .= 'href="'.osc_search_url($search_params).'" class="parent active" data-name="sCategory" data-val="'.$parent['pk_i_id'].'">';
$Link .= '<span class="name">'.$parent['s_name'].'</span>';
$Link .= '<em>('.$Item_Number.')</em>';
$Link .= '</a>';
echo $Link;
?>

$_GET not working on my php file [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I have a url link that has value of the id and process when clicked.
code:
<?php
<a href='settoActive.php?id=".$row['id']."&process=actives' style='font-size:15px;' name='active' value=".$row['id']." class='btn btn-info' />ACTIVE</a>
?>
<a href='settoActive.php?id=".$row['id']."&process=inactive' style='font-size:15px;' name='active' value=".$row['id']." class='btn btn-info' />inactive</a>
and this is where the page will be redirected to:
<?php
$value = $_GET['process'];
echo "<script> alert(".$value.");</script> ";
if($_GET['proc']="actives"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Active' where id=".$id." ") or mysqli0;
echo "<script>alert('Activessss!'); </script>";
//location.replace('addmodel.php')
}
if($_GET['process']="inactive"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Inactive' where id=".$id." ") or mysqli0;
echo "<script>alert('Inactive!'); </script>";
}
// location.replace('addmodel.php')
?>
The problem is the 2 if condition trigger and why it is triggering at the same time?
There are 3 mistakes noticed... 2 places '==' operator and 1 place GET variable name 'process'.
$value = $_GET['process'];
echo "<script> alert(".$value.");</script> ";
if($_GET['process']=="actives"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Active' where id=".$id." ") or mysqli0;
echo "<script>alert('Activessss!'); </script>";
//location.replace('addmodel.php')
}
if($_GET['process']=="inactive"){
$id = $_GET['id'];
$mysqli = new mysqli('10.237.2.152','root','c0k3float','monitoring');
$results = $mysqli->query("UPDATE Shipment_Target SET status='Inactive' where id=".$id." ") or mysqli0;
echo "<script>alert('Inactive!'); </script>";
}
// location.replace('addmodel.php')
?>
Problem in the html code, try following code
<a href='settoActive.php?id=<?php echo $row['id']; ?>&process=actives' style='font-size:15px;' name='active' value="<?php echo $row['id']; ?>" class='btn btn-info' />ACTIVE</a>
<a href='settoActive.php?id=<?php echo $row['id']; ?>&process=inactive' style='font-size:15px;' name='active' value="<?php echo $row['id']; ?>" class='btn btn-info' />inactive</a>

Multiple ID's on single button click

HTML + PHP
<?php
for($i=0;$i<5;$i++){
?>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this.id, $('.copyTarget').attr('id'));">Copy</button>
<?php
}
?>
JS
<script>
function reply_click(clicked_id, target_id) {
alert(clicked_id);
alert(target_id);
}
</script>
What i want
I want to get the both values for copyTarget and copyButton as per loop cycle. It means
If current value of $i = 3
then I want alert values like,
clicked_id = copyTarget3
target_id = copyButton3
What i am currently getting is,
If current value of
$i = 3
then I want alert values like,
clicked_id = copyTarget0
target_id = copyButton3
Its taking first value of ID(copyTarget) stored initially. I want current loop cycle value.
Any help would do
Thanks
Why use JS in handler?
Try:
onClick="reply_click('copyButton<?php echo $i; ?>', 'copyTarget<?php echo $i; ?>')"
Also you should store id names (copyButton and copyTarget) in php variable, so you can change them in one place.
You could try something like below. However, I would go by Maxx's answer .. It really depends on what you plan to do with the rest of code etc.
<?php
for($i=0;$i<5;$i++){
?>
<div>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this)">Copy</button>
</div>
<?php
}
?>
<script>
function reply_click(btn) {
var clicked_id = $(btn).attr('id');
var target_id=$(btn).parent().find('span').html();
alert(clicked_id);
alert(target_id);
}
</script>
Try This
<?php
for($i=0;$i<5;$i++){
?>
<input readonly class="copyTarget" id="copyTarget<?php echo $i; ?>" value="val<?php echo $i; ?>">
<span>
val<?php echo $i; ?>
</span>
<button class="btn btn-primary" id="copyButton<?php echo $i; ?>" onClick="reply_click(this.id);">Copy</button>
<?php
}
?>
JS
<script>
function reply_click(clicked_id) {
alert(clicked_id); //clicked id
alert(clicked_id.split('copyButton').join('copyTarget')); // target id
}
</script>

Autoclose alert doesn't alert

I cannot se why my autoclose doesn't work.
I use php to echo out javascript. I do echo both script. First the "not working" and then the "working". Only the "working" script shows up!
What's wrong?
Not working
echo "<div class='alert-message'></div>";
echo "<script type='text/javascript'>";
echo "$('.alert-message').alert();";
echo "window.setTimeout(function() { $('.alert-message').alert('close'); }, 5000);";
echo "</script>";
Works
//Echo succes
echo "<script type='text/javascript'>";
echo "alert('Välkommen');";
echo 'window.location = "page.php"';
echo "</script>";
die();
.alert-message div is not yet loaded in DOM when you are using it. Try to echo it before script:
echo "<div class='alert-message'></div>";
echo "<script type='text/javascript'>";
echo "$('.alert-message').alert();";
echo "window.setTimeout(function() { $('.alert-message').alert('close'); }, 5000);";
echo "</script>";

why is window.open php fetching variable is fetching same id every time and not the remaining entries?

Here in the code, $fetch['id'] working fine else where but in window.open it is taking same value always that is id number 25, and not the other ids, i can't understand why is it so....Please help........
$i=1;
while ($fetch = mysql_fetch_array($data_qry))
{
echo $url = $_SERVER['REQUEST_URI']."/edit_user_comment.php?id=".$fetch['id']; // here variable working fine....
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$fetch['comp_name']."</td>";
if (strlen($fetch['comp_add'])> 10)
{
echo "<td>".substr($fetch['comp_add'], 0, 10)."...";
}else{
echo "<td>".$fetch['comp_add'];
}"</td>";
echo "<td>".$fetch['business_registration_no']."</td>";
echo "<td>".$fetch['email_id']."</td>";
echo "<td>".$fetch['tel_no']."</td>";
echo "<td>".$fetch['fax']."</td>";
echo "<td>".$fetch['prsn_in_chrg']."</td>";
echo "<td>".$fetch['ph_no']."</td>";
?>
<script>
function windowopen(){
window.open('<?= $url; ?>','Comment Edit', 'menubar=1,location=1,status=1,scrollbars=yes,width=500, height=500');
//here it is takin $fetch['id']=25 only and not other ids, i want to know the mistake i m doing here... Please help
}
</script>
<?php
if($fetch['comments']!='')
{
echo "<td><textarea name='comments' id='id_comments'>".$fetch['comments']."</textarea>
<a onclick='return windowopen();' class='button button-primary button-large edit'>Edit</a></td>";
}else{
echo "<td><a onclick='return windowopen();' class='button button-primary button-large'>Add Comment</a></td>";
}
echo "<td> <a href='response.php?id=".$fetch['id']."&action=approve' class='button button-primary button-large approve'>Approve</a> <a href='response.php?id=".$fetch['id']."&action=dissapprove' class='button button-primary button-large'>Dissapprove</a> </td>";
echo "</tr>";
$i++;
}
To elaborate on my earlier comment - Generated links should look something like:
echo '<td><a onclick="return windowopen(\''.$url.'\');">Add Comment</a></td>';
...and your function should be declared once outside of the loop like :
<script>
function windowopen(url)
{
window.open(url,'Comment Edit', '/*blah*/');
}
</script>
This is because you are redeclaring the same function windowopen() so the last declaration overrides the others and you got the same function for all elements

Categories