on click function not working if html written inside php - php

see my code here
if(isset($_POST['table_category_view'])){
$result = $wpdb->get_results('SELECT id,category FROM '.$ps_category_table_name,ARRAY_A);
$html = "";
foreach ($result as $value){
$id = $value['id'];
$categry = $value['category'];
$html .= '<tr id="catgry-row' . $id . '">';
$html .= '<td id="catgry-row' . $id . '">'.$categry.'</td>';
$html .= '<td><button class="btn btn-primary btn-sm" id="edit-catgry' . $id . '" onclick="edit_category("' . $id . '");">Edit</button>';
$html .= '<button class="btn btn-danger btn-sm" id="delete-catgry' . $id . '" onclick="delete_category("' . $id . '");">Delete</button>';
$html .= '<button class="btn btn-primary btn-sm" style="display:none" id="update-catgry' . $id . '" onclick="update_category("' . $id . '");">Update</button>';
$html .= '<button class="btn btn-warning btn-sm" style="display:none" id="cancel-update-catgry' . $id . '" onclick="cancel_update_category("' . $id . '");">Cancel</button></td></tr>';
}
echo $html;
}
here how to write onclick function in my code it was not working please give me solution

enclose your sentence inside the double quote ("") not single code & write your code as below,
$html = "<td><button class='btn btn-primary btn-sm' id='edit-catgry".$id." ' onclick=edit_category('$id');>Edit</button>";

you have to write the function parameter like this in php.
onclick="edit_category('.$id.');"

Please try below code:
<?php
if(isset($_POST['table_category_view'])){
$result = $wpdb->get_results('SELECT id,category FROM '.$ps_category_table_name,ARRAY_A);
$html = "";
foreach ($result as $value){
$id = $value['id'];
$categry = $value['category'];
$html .= '<tr id="catgry-row' . $id . '">';
$html .= '<td id="catgry-row' . $id . '">'.$categry.'</td>';
$html .= '<td><button class="btn btn-primary btn-sm" id="edit-catgry' . $id . '" onclick="edit_category(' . $id . ');">Edit</button>';
$html .= '<button class="btn btn-danger btn-sm" id="delete-catgry' . $id . '" onclick="delete_category(' . $id . ');">Delete</button>';
$html .= '<button class="btn btn-primary btn-sm" style="display:none" id="update-catgry' . $id . '" onclick="update_category(' . $id . ');">Update</button>';
$html .= '<button class="btn btn-warning btn-sm" style="display:none" id="cancel-update-catgry' . $id . '" onclick="cancel_update_category(' . $id . ');">Cancel</button></td></tr>';
}
echo $html;
}
?>

Related

Is it possible to pass blade directive like #if into a string from laravel

Here is the code i have tried in controller
$categories = Category::where('categories.name', 'LIKE', '%' . $category_name . '%')
->where('categories.category_code', 'LIKE', '%' . $category_code . '%')->get();
$category_data = $categories->count();
if ($category_data > 0) {
$i = 1;
foreach ($categories as $category) {
$output .=
'<tr>
<td>' . $i++ . '</td>
<td><img src="' . asset($category->photo) . '" class="img-fluid" style="width: 170px; object-fit: cover;"></td>
<td>' . $category->name . '</td>
<td>' . $category->category_code . '</td>
<td><a href="' . route("category.edit", $category->id) . '" class="btn btn-warning">
<i class="icofont-ui-settings icofont-1x"></i>
</a>
#if($delete == "yes")
<form action="' . route("category.destroy", $category->id) . '" method="POST" class="d-inline-block" onsubmit="return confirm("Are you sure to delete the item?")">
#csrf
#method("DELETE")
<button class="btn btn-outline-danger" type="submit"><i class="icofont-close icofont-1x"></i></button>
</form>
#endif
</td>
</tr>';
}
return Response($output);
Code in blade file, jquery
$('#filtersearch').click(function() {
var category_name = $('#category_name').val();
var category_code = $('#category_code').val();
// alert(category_name)
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
$.get('/categorysearch', {
category_name: category_name,
category_code: category_code
},
function(data) {
$('#filter_data').html(data);
})
})
the output looks like thisall of the blade directive are showing as string
Is there a way to develop this, any solution would be very greatful.
Thanks!
I think the best way to do this is by using render() method
you can create your view file and pass your data and render the final HTML code
Example
$renderedHtml = view('your_view_file',compact('some_data'))->render();
then you can respond with your rendered HTML
return Response($renderedHtml );
I hope it's helpful
You have to use Concatenation in laravel for parsing string:
foreach ($categories as $category) {
$output .=
'<tr>
<td>' . $i++ . '</td>
<td><img src="' . asset($category->photo) . '" class="img-fluid" style="width: 170px; object-fit: cover;"></td>
<td>' . $category->name . '</td>
<td>' . $category->category_code . '</td>
<td><a href="' . route("category.edit", $category->id) . '" class="btn btn-warning">
<i class="icofont-ui-settings icofont-1x"></i>
</a>';
if($delete == "yes"){
$output .= '<form action="' . route("category.destroy", $category->id) . '" method="POST" class="d-inline-block" onsubmit="return confirm("Are you sure to delete the item?")">';
$output .= '<input type="hidden" name="_token" value="'.csrf_token().'">';
$output .= '<input type="hidden" name="_method" value="DELETE"><button class="btn btn-outline-danger" type="submit"><i class="icofont-close icofont-1x"></i></button>
</form>';
}
$output .='</td></tr>';
}
Starting from laravel 9.0, you can render blade strings on the fly
use Illuminate\Support\Facades\Blade;
//...
$categories = Category::where('categories.name', 'LIKE', '%' . $category_name . '%')
->where('categories.category_code', 'LIKE', '%' . $category_code . '%')->get();
$category_data = $categories->count();
if ($category_data > 0) {
$i = 1;
foreach ($categories as $category) {
$output .=
'<tr>
<td>' . $i++ . '</td>
<td><img src="' . asset($category->photo) . '" class="img-fluid" style="width: 170px; object-fit: cover;"></td>
<td>' . $category->name . '</td>
<td>' . $category->category_code . '</td>
<td><a href="' . route("category.edit", $category->id) . '" class="btn btn-warning">
<i class="icofont-ui-settings icofont-1x"></i>
</a>
#if($delete == "yes")
<form action="' . route("category.destroy", $category->id) . '" method="POST" class="d-inline-block" onsubmit="return confirm("Are you sure to delete the item?")">
#csrf
#method("DELETE")
<button class="btn btn-outline-danger" type="submit"><i class="icofont-close icofont-1x"></i></button>
</form>
#endif
</td>
</tr>';
}
return Blade::render($output, ['delete' => 'yes']);
If you're on laravel 8.x or lower, you can use this package felixdorn/laravel-render-blade-string or the other answers solutions

PHP function to make more values = 0 if an id = 52 (value)

Hello i have a table when i want to display some td value = 0 if the status of the task has the id=52!
Down i will put a picture of the table!
I tried to use this code for that:
if ($status['id']) { $calC = $calCminusrealcost = $realCost =0; }
Here $calC, $calCminusrealcost and $realCost are the values i want to become 0!
Here is the pucture of the table: [!https://i.stack.imgur.com/YWOwb.png][1]][1]
So after i use this code it works but in general for all tasks, not just for those which have the id = 52, and if i try to replace id with 52 in the function it doesn't work!
Does someone know what can I do?
this is the full code i have about status:
$outputStatus = '';
$outputStatus .= '<span class="inline-block label" style="color:' . $status['color'] . ';border:1px solid ' . $status['color'] . '" task-status-table="' . $aRow['status'] . '">';
$outputStatus .= $status['name'];
if ($canChangeStatus) {
$outputStatus .= '<div class="dropdown inline-block mleft5 table-export-exclude">';
$outputStatus .= '<a href="#" style="font-size:14px;vertical-align:middle;" class="dropdown-toggle text-dark" id="tableTaskStatus-' . $aRow['id'] . '" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">';
$outputStatus .= '<span data-toggle="tooltip" title="' . _l('ticket_single_change_status') . '"><i class="fa fa-caret-down" aria-hidden="true"></i></span>';
$outputStatus .= '</a>';
$outputStatus .= '<ul class="dropdown-menu dropdown-menu-right" aria-labelledby="tableTaskStatus-' . $aRow['id'] . '">';
foreach ($task_statuses as $taskChangeStatus) {
if ($aRow['status'] != $taskChangeStatus['id']) {
$outputStatus .= '<li>
<a href="#" onclick="task_mark_as(' . $taskChangeStatus['id'] . ',' . $aRow['id'] . '); return false;">
' . _l('task_mark_as', $taskChangeStatus['name']) . '
</a>
</li>';
}
}
if ($status['id']) {
$calC = $calCminusrealcost = $realCost =0;
}
$outputStatus .= '</ul>';
$outputStatus .= '</div>';
}
$outputStatus .= '</span>';
$row[] = $outputStatus;
[1]: https://i.stack.imgur.com/YWOwb.png

If statment to echo an image if var=EUR doesnt work

This code added currency switcher to a menu,
But unfortunately I don't have access to MySQL database so I can't add image "flag" to each currency, then execute them
So I tried to use if statement.
This is my code:
$currency_switcher_enable = houzez_option('currency_switcher_enable');
$is_multi_currency = houzez_option('multi_currency');
if( $currency_switcher_enable != 0 && $is_multi_currency != 1 ) {
if (class_exists('FCC_Rates')) {
$supported_currencies = houzez_get_list_of_supported_currencies();
if (0 < count($supported_currencies)) {
$current_currency = houzez_get_wpc_current_currency();
echo '<li class="btn-price-lang btn-price">';
echo '<form id="houzez-currency-switcher-form" method="post" action="#" class="open">';
echo '<button id="houzez-selected-currency" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span>' . $current_currency . '</span> <i class="fa fa-sort"></i></button>';
echo '<ul id="houzez-currency-switcher-list" class="dropdown-menu" aria-labelledby="dropdown" style="display:none;">';
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
if (data-currency-code='EUR') {
echo '<img src="images/euro-flag.png"';
if (data-currency-code='TR') {
echo '<img src="images/turkish-flag.png"';
}
}
echo '</ul>';
echo '<input type="hidden" id="houzez-switch-to-currency" name="houzez_switch_to_currency" value="' . $current_currency . '" />';
echo '<input type="hidden" id="currency_switch_security" name="nonce" value="' . wp_create_nonce('houzez_currency_converter_nonce') . '"/>';
echo '</form>';
echo '</li>';
}
}
}
?>
But it doesn't work? What I'm doing wrong?
This is the original code:
$currency_switcher_enable = houzez_option('currency_switcher_enable');
$is_multi_currency = houzez_option('multi_currency');
if( $currency_switcher_enable != 0 && $is_multi_currency != 1 ) {
if (class_exists('FCC_Rates')) {
$supported_currencies = houzez_get_list_of_supported_currencies();
if (0 < count($supported_currencies)) {
$current_currency = houzez_get_wpc_current_currency();
echo '<li class="btn-price-lang btn-price">';
echo '<form id="houzez-currency-switcher-form" method="post" action="#" class="open">';
echo '<button id="houzez-selected-currency" class="btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><span>' . $current_currency . '</span> <i class="fa fa-sort"></i></button>';
echo '<ul id="houzez-currency-switcher-list" class="dropdown-menu" aria-labelledby="dropdown" style="display:none;">';
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
}
echo '</ul>';
echo '<input type="hidden" id="houzez-switch-to-currency" name="houzez_switch_to_currency" value="' . $current_currency . '" />';
echo '<input type="hidden" id="currency_switch_security" name="nonce" value="' . wp_create_nonce('houzez_currency_converter_nonce') . '"/>';
echo '</form>';
echo '</li>';
}
}
}
?>
You have many fails here:
The first is as #kerbh0lz mentioned, the "=" and "==" is not the same purpose. here yours is wrong.
Second your second currency If is in the 1st Currency If so it wont ever pass by.
Third you are comparing a string without quote or a var without $ before data-currency-code so instead you should use $currency_code
Try this:
foreach ($supported_currencies as $currency_code) {
echo '<li data-currency-code="' . $currency_code . '">' . $currency_code . '</li>';
if ($currency_code =='EUR')
echo '<img src="images/euro-flag.png"';
elseif ($currency_code == 'TR')
echo '<img src="images/turkish-flag.png"';
}

How to separate php from html and seperate php again?

while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["orderNumber"].'</td>
<td>'.$row["orderDate"].'</td>
<td>'.$row["requiredDate"].'</td>
<td>'.$row["status"].'</td>
<td><a title="Click To Edit Order Detail" rel="facebox" href="editorderdetail.php?id=' .$row["orderNumber"];.'"><button class="btn btn-warning btn-mini"><i class="icon-edit"></i> Edit </button></a>
<button class="btn btn-danger btn-mini"><i class="icon-trash"></i> Delete</button></td>
</tr>
';
}
I expect to get the $row["orderNumber"] in the url for edit and delete purpose.
You should consider using an editor with syntax highlighting, so the obvious errors are easily fixed. Try following:
while ($row = mysqli_fetch_array($result)) {
$output .= '<tr><td>' .
$row["orderNumber"] . '</td><td>' .
$row["orderDate"] . '</td><td>' .
$row["requiredDate"] . '</td><td>' .
$row["status"] . '</td><td>' .
'<a title="Click To Edit Order Detail" rel="facebox" href="editorderdetail.php?id=' . $row["orderNumber"] . '">' .
'<button class="btn btn-warning btn-mini"><i class="icon-edit"></i> Edit </button></a>' .
'<a href="#" id="' . $row["orderNumber"] . 'class="delete" title="Click To Delete">' .
'<button class="btn btn-danger btn-mini"><i class="icon-trash"></i> Delete</button></a>' .
'</td></tr>';
}

Wordpress Coding Issue

Being a complete numpty at this....
I am having troubles getting the below code to work (just shows the white screen of death)
case 'venue' :
if( self::isValid($atts['venue'])) {
$output .= '<td>' . apply_filters( 'ecs_event_list_venue', tribe_get_venue(), $atts ) . '</span></td>';
if( !empty(tribe_get_event_website_link()) ) {
$output .= '<td><a class="btn btn-danger btn-small" href="' . tribe_get_event_website_link() . '">Bookings</a></td>';
} else {
$output .= '<td><a class="btn btn-danger btn-small" href="' . tribe_get_event_link() . '">Read More</a></td>';
}
}
}
break;
The original code was
case 'venue' :
if( self::isValid($atts['venue'])) {
$output .= '<td>' . apply_filters( 'ecs_event_list_venue', tribe_get_venue(), $atts ) . '</span></td>';
}
break;
This works fine..
Any assistance appreciated and respected!!
You can try this :
if( self::isValid($atts['venue'])) {
$output .= '<td>' . apply_filters( 'ecs_event_list_venue', tribe_get_venue(), $atts ) . '</span></td>';
$is_value = tribe_get_event_website_link();
if( !empty($is_value) ) {
$output .= '<td><a class="btn btn-danger btn-small" href="' . tribe_get_event_website_link() . '">Bookings</a></td>';
} else {
$output .= '<td><a class="btn btn-danger btn-small" href="' . tribe_get_event_link() . '">Read More</a></td>';
}
}
You can't use function return value like this.

Categories