A non-numeric value encountered with Laravel - laravel

I hope you're well. I'm trying to display a message with the search value I've done (I have 3 filed for a search : keyword, article type and date).
I have a function inside my Controller :
public function page_liste_resultat_recherche_article(Request $request)
{
$recherche_effectuer = request()->query('rechercher');
$type_article_recherche = request()->query('type_article');
$date_rechercher = request()->query('date');
$article_rechercher = Article::withTrashed()->when($recherche_effectuer, function($query, $recherche_effectuer){
return $query->where('titre', 'like', "%{$recherche_effectuer}%");
})
->when($type_article_recherche, function($query, $type_article_recherche){
return $query->where('type_article_id', 'like', "%{$type_article_recherche}%");
})
->when($date_rechercher, function($query, $date_rechercher){
return $query->where('created_at', 'like', "%{$date_rechercher}%");
})->orderBy('created_at', 'desc')->paginate(5);
if((empty($recherche_effectuer)) && (empty($type_article_recherche)) && ($date_rechercher)){
$message_resultat_recherche = "Résultat pour la date " + ($date_rechercher) + " :";
} elseif((empty($recherche_effectuer)) && (!empty($type_article_recherche)) && (empty($date_rechercher))){
$message_resultat_recherche = "Résultat pour le type d'article " + ($type_article_recherche) + " :";
} elseif((empty($recherche_effectuer)) && (!empty($type_article_recherche)) && (!empty($date_rechercher))){
$message_resultat_recherche = "Résultat pour la date " + ($date_rechercher) + " et le type d'article " + ($type_article_recherche) + " :";
} elseif((!empty($recherche_effectuer)) && (empty($type_article_recherche)) && (empty($date_rechercher))){
$message_resultat_recherche = "Résultat pour le mot cle " + ($recherche_effectuer) + " :";
} elseif((!empty($recherche_effectuer)) && (empty($type_article_recherche)) && (!empty($date_rechercher))){
$message_resultat_recherche = "Résultat pour le(s) mot(s) clé(s) " + ($recherche_effectuer) + " et pour la date " + ($date_rechercher) + " :";
} elseif((!empty($recherche_effectuer)) && (!empty($type_article_recherche)) && (empty($date_rechercher))){
$message_resultat_recherche = "Résultat pour le(s) mot(s) clé(s) " + ($recherche_effectuer) + "et le type d'article " + ($type_article_recherche) + " :";
} elseif((!empty($recherche_effectuer)) && (!empty($type_article_recherche)) && (!empty($date_rechercher))){
$message_resultat_recherche = "Résultat pour le(s) mot(s) clé(s) " +($recherche_effectue) + ", le type d'article " + ($type_article_recherche) + " et la date " + ($date_rechercher) + " :";
} else {
$message_resultat_recherche = "Vous n'avez saisi aucune donnée";
}
return view('admin/article/admin_liste_resultat_recherche', [
'articles' => $article_rechercher,
'resultat' => $message_resultat_recherche
]);
}
Inside my view I show this :
<div class="row d-flex justify-content-center">
<div class="col-md-12">
<p class="text-center mt-3">{{$resultat}}</p>
</div>
</div>
I've this error :
A non-numeric value encountered
Did someone see where I'm wrong please ?
Cordially

Warning: A non-numeric value encountered
As #apokryfos said in a comment, don't concatenate strings with '+' sign. Use '.' instead.
i.e
// Use...
echo "Hello". " world! ". 508;
// Instead of...
echo "Hello"+ " world! "+ 508;

Related

Ajax function is never called inside dynamically generated buttons

This loop generate buttons in my HTML inside an AJAX function.
for( var i = 0; i<len; i++){
var d = new Date(response[i]['date']);
dformat = [d.getDate() + '/' +
(d.getMonth()+1) + '/' +
d.getFullYear() + ' ' +
d.getHours() + ':' +
d.getMinutes()];
console.log(response[i])
var bons = "<tbody>" +
"<th>" + response[i]['id'] + "</th>" +
"<td>" + dformat + "</td>" +
"<td>" + response[i]['poids'] + "</td>" +
"<td>" + response[i]['origine'] + "</td>" +
"<td>" + response[i]['type'] + "</td>" +
"<td>" + response[i]['famille'] + "</td>"
if(response[i]['localisation'] != "Expédié"){
bons = bons + "<td> <input class='evacuate' id='" + response[i]['id'] + "' type='button'" + "value='Evacuer'> </td>"
}else{
bons = bons + "<td>" + response[i]['localisation'] + "</td>"
}
bons = bons + "</tr>" +
"</tbody>"
$("#index").append(
bons
);
}
Then I have another AJAX function which makes every buttons previously generated clickable like :
$(".evacuate").on('click', function(){
console.log("test des boutons")
});
Problem is, the console.log isn't displayed, as if the function is never called. What am I doing wrong here ?
Try this:
$(document).on('click','.evacuate', function(){
console.log("test des boutons")
});
You should define the click event on elements that already exist on the page. So for your situation, select the parent element and then add click event. For example if the element with evacuate class is inside an element with someclass class, this will work:
$(".someclass").on('click', '.evacuate', function(){
console.log("test des boutons")
});
According to your code, you can use tbody tag as parent:
$("tbody").on('click', '.evacuate', function(){
console.log("test des boutons")
});
I don't really understand how this works but thanks to the answers given above I managed to display my console.log.
$(document.getElementsByName).on('click', '.evacuate', function(){
console.log("Test des boutons")
});

Codeigniter - $sOrder and $sLimit in datatables

I'm retrieving records using Codeigniter and datatables. Removing $sOrder and $sLimit loads the data but on filtering, there's a database error:
"You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ''desc' LIMIT '0', '10'' at line 5
SELECT SQL_CALC_FOUND_ROWS id, FName, LName, status, authorizedby, userName
FROM users
ORDER BY id 'desc'
LIMIT '0', '10'"
Here's the code:
if (isset($_REQUEST['iSortCol_0'])) {
$sOrder = "ORDER BY ";
for ($i = 0; $i < intval($_REQUEST['iSortingCols']); $i++) {
if ($_REQUEST['bSortable_' . intval($_REQUEST['iSortCol_' . $i])] == "true") {
$sOrder .= $aColumns[intval($_REQUEST['iSortCol_' . $i])] . "
" . $this->db->escape($_REQUEST['sSortDir_' . $i]) . ", ";
}
}
$sOrder = substr_replace($sOrder, "", -2);
if ($sOrder == "ORDER BY") {
$sOrder = "";
}
}
$sWhere = "";
// this for search code
if ($_REQUEST['sSearch'] != "") {
$sWhere = "WHERE (";
for ($i = 0; $i < count($aColumns); $i++) {
$sWhere .= $aColumns[$i] . " LIKE '%" . $this->db->escape($_REQUEST['sSearch']) . "%' OR ";
}
$sWhere = substr_replace($sWhere, "", -3);
$sWhere .= ')';
}
for ($i = 0; $i < count($aColumns); $i++) {
if ($_REQUEST['bSearchable_' . $i] == "true" && $_REQUEST['sSearch_' . $i] != '') {
if ($sWhere == "") {
$sWhere = "WHERE ";
} else {
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i] . " LIKE '%" . $this->db->escape($_REQUEST['sSearch_' . $i]) . "%' ";
}
}
// generate sql query
$sQuery = "SELECT SQL_CALC_FOUND_ROWS " . str_replace(" , ", " ", implode(", ", $aResultColumns)) . "
FROM $sTable
$sWhere
$sOrder
$sLimit
";
Now, removing the last two lines ($sOrder and $sLimit) works for loading the data but error on filtering. How is this fixable.
This is how I solved it:
$sWhere = "";
// this for search code
if ($_REQUEST['sSearch'] != "") {
$sWhere = "WHERE (";
for ($i = 0; $i < count($aColumns); $i++) {
$sWhere .= $aColumns[$i] . " LIKE '*%" . $this->db->escape($_REQUEST['sSearch']) . "%*' OR ";
}
$sWhere = substr_replace($sWhere, "", -3);
$sWhere .= ')';
}
for ($i = 0; $i < count($aColumns); $i++) {
if ($_REQUEST['bSearchable_' . $i] == "true" && $_REQUEST['sSearch_' . $i] != '') {
if ($sWhere == "") {
$sWhere = "WHERE ";
} else {
$sWhere .= " AND ";
}
$sWhere .= $aColumns[$i] . " LIKE '*%" . $this->db->escape($_REQUEST['sSearch_' . $i]) . "%*' ";
}
}
$sOrder = str_replace("'", "", $sOrder);
$sLimit = str_replace("'", "", $sLimit);
$sWhere = str_replace("'", "", $sWhere);
$sWhere = str_replace("*", "'", $sWhere);
// generate sql query
$sQuery = "SELECT SQL_CALC_FOUND_ROWS " . str_replace(" , ", " ", implode(", ", $aResultColumns)) . "
FROM $sTable
$sWhere
$sOrder
$sLimit
";```

required on selectpicker based on div

I am learner in javaScript
How to write required on selectpicker based on
var typeSelectBox = "<select id='depprojsDevice' class='selectpicker'>";
typeSelectBox += "<option value=''> -- SELECT --</option>";
if (arr) {
for (var i in arr) {
typeSelectBox += "<option value='" + arr[i][2] + "'>" + arr[i][0] + " " + arr[i][1] + "</option>";
}
}
typeSelectBox + "</select>";
$('#depprojsDevice_Div').html(typeSelectBox);
$("div#depprojsDevice_Div select[id = 'depprojsDevice']").attr('required','required');
$(function(){
$("#depprojsDevice").prop('required',true);
});
Try This Code

Wordpress ajax: submit form then go to homepage instead of replacing html tag

I tried many times but it does not work as I expect to. I have a form submit a company name back to wordpress ajax to query AD in order to get a list of users. However, everytime I hit submit button, it just loads the homepage of the website. Please look at and show me the incorrect point in my code
Here is the code in function.php
function load_scripts() {
wp_enqueue_script('jquery');
wp_register_script('directory_script',get_template_directory_uri().'/js/directory.js',array('jquery'));
wp_enqueue_script('directory_script');
wp_register_script('directory1_script',get_template_directory_uri().'/js/directory1.js',array('jquery'));
wp_enqueue_script('directory1_script');
}
add_action ('wp_enqueue_scripts','load_scripts');
// buildDirectory function
function buildDirectory () {
$Company = $_GET["companyname"];
$ad = "ad1.p.local";
$result1;
exec("ping -n 3 " . $ad, $output, $result1);
if ($result1 != 0) {
$ad = "ad2.p.local";
exec("ping -n 3 " . $ad, $output, $result1); }
elseif ($result1 != 0) {
$ad = "ad3.p.local";
exec("ping -n 3 " . $ad, $output, $result1); }
//else { echo "Unable to get contact"; }
$ldap_connection = ldap_connect($ad);
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die("Unable to set LDAP protocol version");
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
$result;
$ldap_password = "abc";
$ldap_username = "cde";
$bind = ldap_bind($ldap_connection, $ldap_username, $ldap_password);
//$Company = "Patrick Industries";
//if (TRUE === $bind)
// {echo "Binding is successful<br />";}
//else {echo "Binding is unsuccessful<br />";}
if (TRUE === $bind) {
$ldap_base_dn = "DC=p,DC=local";
$search_filter = "(&(objectCategory=person)(objectClass=user)(company=$Company))";
$attributes = array();
$attributes[] = 'givenname';
$attributes[] = 'mail';
$attributes[] = 'samaccountname';
$attributes[] = 'sn';
$attributes[] = 'telephonenumber';
$attributes[] = 'company';
$result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
if (FALSE !== $result) {
$entries = ldap_get_entries($ldap_connection, $result);
// echo "<br />Found " . $entries["count"] . " entries <br />";
echo "<h4>List of contacts:</h4><br />";
echo "<table><tr>
<th style='font-weight:bold;font-size:16px;width:150px'>Name</th>
<th style='font-weight:bold;font-size:16px;width:120px'>Phone</th>
<th style='font-weight:bold;font-size:16px;width:220px'>Email</th>
<th style='font-weight:bold;font-size:16px;width:160px'>Company</th>
</tr><tr>";
$count1 = 0;
for ($x=0; $x<$entries['count']; $x++){
if (!empty($entries[$x]['givenname'][0]) &&
!empty($entries[$x]['mail'][0]) &&
!empty($entries[$x]['samaccountname'][0]) &&
!empty($entries[$x]['telephonenumber'][0]) &&
!empty($entries[$x]['sn'][0]))
{ echo "<td>". $entries[$x]['givenname'][0] . " " . $entries[$x]['sn'][0] . "</td>";
echo "<td>". $entries[$x]['telephonenumber'][0] . "</td>";
echo "<td>" . $entries[$x]['mail'][0] . "</td>";
echo "<td>". $entries[$x]['company'][0] . "</td></tr>";
$count1++; }
// { $user = $entries[$x]['givenname'][0] . " " . $entries[$x]['sn'][0] . " , " . $entries[$x]['telephonenumber'][0] . " , " . "" . $entries[$x]['mail'][0] . "" . " , " . $entries[$x]['company'][0] . " .<br />";}
}
// echo "There are " . $count1 . " contacts";
echo "</table>";
}
}
die();
}
add_action('wp_ajax_bDirectory','buildDirectory');
add_action('wp_ajax_nopriv_bDirectory','buildDirectory');
Here is js file
// Have to include in .js file
var $j = jQuery.noConflict();
var ajaxurl = "http://<ip>/wordpress/wp-admin/admin-ajax.php";
// build directory through form submission
$j("#company").submit(function(){
//var cname = $j("#cname").val();
var str = $(this).serialize();
$j.ajax({
url: ajaxurl,
type: "GET",
data: { action: "bDirectory", "cname": str },
success: function(html) {
$j("#company").replaceWith(html);
$j("#load").hide();
}
});
});
Here is HTML code
<form id="company" action="#">
<input id="cname" name="companyname" type="text"/>
<button type="submit">Submit</button>
</form>
You have to prevent the form from being submitted "the normal way":
$j("#company").submit(function(e){
e.preventDefault(); // Prevents the form default action to be triggered
var str = $(this).serialize();
$j.ajax({
url: ajaxurl,
type: "GET",
data: { action: "bDirectory", "cname": str },
success: function(html) {
$j("#company").replaceWith(html);
$j("#load").hide();
}
});
});
EDIT
In functions.php you use $_GET["companyname"]; but in your javascript you send data: { action: "bDirectory", "cname": str },, this should be corrected before anything.

Ajax requests in a loop- refresh div after each request not working in IE8

I have a loop in which I'm making Ajax xmlhttp requests. This occurs within a function triggered by a window.onload event.
The Ajax calls are being made with async=false, because they need to occur in a specific order that relies on each step completing before the next can occur.
With each successive request in the loop, I'm updating a div with the xmlhttp.responseText.
Firefox is refreshing between calls as desired.
IE is not. When the loop begins, the div is populated with the pre-loop content. When the loop finishes, the div is populated with the first refresh that occurs outside of the loop.
Can someone please help?
Two attempted solutions:
1. Adding a random string to the end of the GET query string to ensure a unique url
2. Submitting with the POST method
No luck with either.
Thanks.
Code...
<script type="text/javascript">
function order_process() {
var err;
var queue_id = "<?= implode(':',$plans[$_REQUEST['order_queue_id']]); ?>".split(':'); // Queue ID
var queue_ax = "<?= implode(':',array_keys($plans[$_REQUEST['order_queue_id']])); ?>".split(':'); // Queue Action
i = 0;
for (step in queue_id) {
// The DIV contents that display during each loop iteration
document.getElementById("barber_pole").innerHTML='\
<center>\
<table style="align:left" border="0" cellpacing="1" cellpadding="1">\
<tr><td><B>Processing Order</B><span style="float:right;">Step ' + (i + 1) + '/' + queue_id.length + '</span></td></tr>\
<tr><td style="background-color:#FFFFFF;height:1.5px"></td></tr>\
<tr><td height="20" style="text-align:center">' + queue_ax[i] + '...</td></tr>\
<tr><td height="20"><IMG SRC="../../_include/images/barber_pole.gif" style="vertical-align: middle;"></td></tr>\
</table>\
</center>';
xmlhttp = ajax_request(); // Create request object
xmlhttp.onreadystatechange=function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
1;
}
}
var url = '../../../api/order_process.php?api_login=' + "<?=$api_login?>" + '&api_pass=' + "<?=$api_pass?>" + "&order_id=<?= $_REQUEST['order_id']?>" + '&order_action_id=' + queue_id[step] + '&timeid=' + Math.random();
xmlhttp.open("GET",url,false);
xmlhttp.send();
// If the response includes the string 'failed' exit the loop and render error error message
if (xmlhttp.responseText.split(',')[0] == 'failed') {
err = queue_ax[i] == 'Registering Domain'
? "<h1 class=\"landing-title\">" + fname + ', ' + "<?=$feedback[domain_register][title]?>" + "</h1><DIV class='landing-body'><?=$feedback[domain_register][body]?></DIV>"
: queue_ax[i] == 'Provisioning cPanel Account'
? "<h1 class=\"landing-title\">" + fname + ', ' + "<?=$feedback[cpanel_provision][title]?>" + "</h1><DIV class='landing-body'><?=$feedback[cpanel_provision][body]?></DIV>"
: queue_ax[i] == 'Credit Card Fraud Protection'
? "<h1 class=\"landing-title\">" + fname + ', ' + "<?=$feedback[maxmind_minfraud][title]?>" + "</h1><DIV class='landing-body'><?=$feedback[maxmind_minfraud][body]?>\"" + xmlhttp.responseText + '"</DIV>'
: queue_ax[i] == 'Verifying Payment'
? "<h1 class=\"landing-title\">" + fname + ', ' + "<?=$feedback[verify_payment][title]?>" + "</h1><DIV class='landing-body'><?=$feedback[verify_payment][body]?>\"" + xmlhttp.responseText + '"</DIV>'
: xmlhttp.responseText == 'failed,'
? "<h1 class=\"landing-title\">" + fname + ', ' + "<?=$feedback[gen_err][title]?>" + "</h1><DIV class='landing-body'><?=$feedback[gen_err][body]?></DIV>"
: "<h1 class=\"landing-title\">" + fname + ', ' + "<?=$feedback[gen_err][title]?>" + "</h1><DIV class='landing-body'><?=$feedback[gen_err][body]?>\"" + xmlhttp.responseText + '"</DIV>';
break;
}
i++;
}
if (err) {
document.getElementById("landing-pres").innerHTML = err;
Cufon.replace('.landing-title');
} else {
document.getElementById("barber_pole").innerHTML = "<?= $thank[$_REQUEST['order_queue_id']][1] ?>";
}
}
window.onload=order_process;
</script>
async=false is not recommended [third parameter in xmlhttp.open("GET",url,false);], and we should not write onreadystatechange function if we are using async=false [Also, you are doing nothing in that function]. Please refer http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
Here is the good way that collecting / preparing all HTML once and update your "barber_pole" div like this..
<script type="text/javascript">
function order_process()
{
var err;
var queue_ids = "<?php echo implode(':',$plans[$_REQUEST['order_queue_id']]); ?>"; // Queue IDs separated by ':'
var queue_axns = "<?php echo implode(':',array_keys($plans[$_REQUEST['order_queue_id']])); ?>"; // Queue Actions separated by ':'
var xmlhttp = ajax_request(); // Create request object
var url = '../../../api/order_process.php?api_login=' + "<?=$api_login?>" + '&api_pass=' + "<?=$api_pass?>" + "&order_id=<?= $_REQUEST['order_id']?>" + '&order_action_ids=' + encodeURIComponent(queue_ids) + '&order_actions=' + encodeURIComponent(queue_axns) + '&timeid=' + Math.random();
xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readyState == 4)
{
document.getElementById("barber_pole").innerHTML = xmlhttp.responseText;;
}
else
{
document.getElementById("barber_pole").innerHTML = "Processing..";
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
window.onload=order_process;
</script>
in order_process.php file...
<?php
//Checking whether logged in or not blah blah..
$action_ids = $_REQUEST['order_action_ids'];
$order_actions = $_REQUEST['order_actions'];
$actions_ids_array = explode(":", $action_ids);
$order_actions_array = explode(":", $order_actions);
$strHTML = '';
for($cnt=0; $cnt < count($actions_ids_array); $cnt++ )
{
$each_action_id = $actions_ids_array[$cnt];
$each_action = $order_actions[$cnt];
//Process each action collect $fname and $status value and generate HTML
//blah blah..
$strHTML .= "<h1 class=\"landing-title\">" . $fname . $status . "</h1>";
}
echo $strHTML;
exit;

Resources