reading a url in PHP that has been encoded in javascript - javascript
In Javascript i encode parts of the request parameter like this
window.location.href = "search.php?qry=" + encodeURIComponent("m & l");
In my Search.php i reach this like this
$url = urldecode($_SERVER['REQUEST_URI']);
echo ."Full URL: " .$url ."<br>";
$parts = parse_url($url);
parse_str($parts['query'], $query);
$qry = "Just Query: " .trim($query['qry']);
echo $qry ."<br>";
This prints out:
Full Url: /Search.php?qry=m & l
Just Query: m
Looks like the stuff after the & is being dropped in the 'm & l`
What changes do i need to make in PHP or Javascript?
Just change:
$url = urldecode($_SERVER['REQUEST_URI']);
to
$url = $_SERVER['REQUEST_URI'];
You're basically double-decoding as parse_url will decode it as well.
Worth noting that PHP has already done this for you so there's not really a reason to parse your own URL. $_GET['qry'] will contain 'm & l'
If you're doing this for several query variables, you'll need to run encodeURIComponent separately for each.
Example:
window.location.href = "search.php?qry=" + encodeURIComponent("m & l") + "&subcat="+encodeURIComponent("hello & there");
You're explicitly telling it to encode the & after all.
Related
AJAX won't call the PHP file when using $.post()
I have been trying to export a search result to an Excel file (type .xls), before this, I have been using purely PHP and it works. However, my client requests to have "live search" effect, so I have to shift to AJAX. Here is the starting point: User clicks "Export" button, and in the javascript (in the main php file viewdata.php): <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js'></script> .... $(document).ready(function () { var guid = <?php echo $guid ?>; var date = document.getElementById("cbXDate").value; var key = document.getElementById("cbsearch").value; console.log("GUID: '" + guid + "', Date: '" + date + "' Key: '" + key + "'"); $.post("export_contacts.php", { sGuid: guid, sDate: date, sKey: key }, function () { console.log("Complete"); } ); }); cbXDate is an input field of type date to let user choose a date from whence to export the data, and cbsearch is a text input field to include a search keyword. console commands are added to see where the code execution has went through. in the export_contact.php: <?php echo '<script> console.log("Export PHP activated."); </script>'; ?> I removed the PHP MySQL data selection code just to debug the problem (full source code below). Problem is: export_contacts.php is never called. The "Export PHP activated" message never popped up in the console. The console only displayed the data values and "Completed", i.e. export_contacts.php was never called. Output: GUID: '0001', Date: '2021-08-01' Key: 'Jo' Complete Out of curiosity, I replaced $.post(...) with $("#export_div").load(...) and the console message showed up: $(document).ready(function () { var guid = <?php echo $guid ?>; var date = document.getElementById("cbXDate").value; var key = document.getElementById("cbsearch").value; console.log("GUID: '" + guid + "', Date: '" + date + "' Key: '" + key + "'"); $("#export_div").load("export_contacts.php", { sGuid: guid, sDate: date, sKey: key }, function () { console.log("Complete"); } ); }); Output: GUID: '0001', Date: '2021-08-01' Key: 'Jo' Export PHP activated. Complete But this is not what I want, I want to write the output to a file, not display them in a div in the webpage. However, the data shown in the "export_div" div is correct, but the header part is not running, I know the quirkyness in header() calls, but I didn't output anything before the header() calls (unless output from the calling viewdata.php file also count?), here is the full export_contacts.php source code: <?php include("./php/auth.php"); $guid = $_POST['sGuid']; $date = $_POST['sDate']; $skey = $_POST['sKey']; $searchKey = $_POST['sKey']; if($searchKey == "") { $skey = "'%'"; } else { $skey = "'%".$searchKey."%'"; } $sql = "SELECT *, FROM_UNIXTIME(ROUND((date / 1000), 0) + 46800) AS date FROM contacts WHERE owner = '$guid' AND contact <> '' AND (contact LIKE $skey OR name LIKE $skey) "; if(!empty($date)) { "AND date >= '$date' "; } $sql .= "ORDER BY contact;"; if($result = mysqli_query($link, $sql)) { $columnHeader = ''; $columnHeader = "Owner" . "\t" . "Contact" . "\t" . "Name" . "\t" . "SaveDate" . "\t"; $setData = ''; while($rows = mysqli_fetch_assoc($result)) { $rowData = ''; foreach ($rows as $value) { $value = '"' . $value . '"' . "\t"; $rowData .= $value; } $setData .= trim($rowData) . "\n"; } // in case of .load() used, // code works up until this point // code doesn't work since here... header("Content-type: application/xls"); header("Content-Disposition: attachment; filename=contact_".$guid.".xls"); header("Pragma: no-cache"); header("Expires: 0"); echo ucwords($columnHeader) . "\n" . $setData . "\n"; // until here // this will show in console in case of .load() used echo '<script> console.log("Export PHP activated."); </script>'; die(); } else { echo "<script>window.alert('ERROR: '".mysqli_error($link).")</script>"; } include("./php/cleanup.php"); ?> This code is working in the pure PHP version. I don't know why this header() part isn't working in here, could be due to its output got redirected to the div? To make things clear, my question is: "Why $.post(...) isn't calling the PHP file, while $("#export_div").load(...) did?". The header() part is just a sub question, and is fine if it's ignored.
As Kmoser pointed out, I was doing things wrong. None of the tutorial sites I visited did mention that $.post() will not return any result at all, while my php code is expecting the return of the search result and write them in a file in the header() calls.
how to read html file data as array in php
My 1 file is city.html which contains the following code <script language="javascript">alert("Page Called"); </script> 'Bhubaneshwar', 'Orissa', 'India' My another file index.php contains the following code $x=file_get_contents("city.html"); $x=array($x); echo $x[0]; It shows the following output 'Bhubaneshwar', 'Orissa', 'India' But I want single word output like this. When I print $x[0], it should be Bhubaneshwar When I print $x[1], it should be Orissa When I print $x[2], it should be India
First you need to remove script tags $x = file_get_contents("city.html"); $x = preg_replace('%<script[^>]*>.*?</script>%/m', '', $x); and then you can use explode: $array = explode(',', $x); then you can trim and remove quotes: $array = array_map(function($item) { return preg_replace("/^'|'$/", trim($item)); }, $array);
Use explode to convert string to array:- // Remove script tag $string = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $x); // $string = str_replace(' ', '', $string); // Remove space from string $string = preg_replace('/\s+/', '', $string); // Remove whitespace from string // explode string $x = explode(',',$string); Hope it will help you :)
Try, $x=file_get_contents("city.html"); $x=array($x); $html = preg_replace('#<script[^>]*?.*?</script>#', '', $x); $str = str_replace(', ', ',', $html); $x = explode(',',trim($str[0])); $remove[] = "'"; $result = str_replace( $remove, "", $x ); foreach($result as $cities) { echo $cities . "<br>"; }
Here is code that first removes the preceding script tag(s) and breaks down the remainder into an array: $x=array_pop(explode('</script>', $x)); $x=preg_split("/'\s*,\s*'/", trim(trim($x), "'")); The first of those two statements is only needed if you keep that script tag inside your HTML, which seems to be there for testing only. To display the result, you could do this: foreach($x as $item) { echo $item . "<br>"; } Output: Bhubaneshwar Orissa India Considerations It is unusual to have such data format inside HTML files. HTML is intended for rendering data in a user-friendly manner, and that representation does not really fit. If you are the creator of the HTML file, then consider moving to a JSON format. In that case your file would be named city.json and would have this content (the double quotes and brackets are required): ["Bhubaneshwar", "Orissa", "India"] And the code would make use of JSON functions like this: $json=file_get_contents("city.json"); $x=json_decode($json); This way you really use standards, and the code is compact. You could again display the contents of $x like before: foreach($x as $item) { echo $item . "<br>"; } Output: Bhubaneshwar Orissa India If you are the creator of that file, and use PHP to create it, then make use of JSON functions as well, as follows: $x = array("Bhubaneshwar", "Orissa", "India"); file_put_contents ("city.json", json_encode($x));
Generate new hash for each user email using PHP and Javascript
I am using Bootstrap Data Table to generate a user table and I want to display each users avatar in the table which I have working now. I allow users to use gravatar, but I am also forcing the default to be the retro icon. The problem that I have is that it generates the same hash for each user causing their icons to be the same. Here is my JS code: function usernameFormatter(value, row) { var id = row.id; var email = row.email; var avatarType = row.avatar_type; var avatar = '../users/' + row.avatar; if(avatarType == 'Gravatar'){ var retro = 'retro'; var urlencode = <?php echo urlencode( retro ); ?>; var hash = '<?php echo md5( strtolower( trim( email ) ) ); ?>'; var avatar = 'http://www.gravatar.com/avatar/' + hash + '?d=' + urlencode; } return '<img src="' + avatar + '" style="width: 32px;margin-right: 5px;">' + value + ''; } I am having trouble writing, for each email address use a different hash. Can someone guide me in the right direction? Any code examples you have is much appreciated.
var email = email; var urlencode = <?php echo urlencode( retro ); ?>; var hash = '<?php echo md5( strtolower( trim( email ) ) ); ?>'; The problem is you can't use a JS variable in PHP without passing it to the server first. When PHP is processing, it has no idea what email is.
how? can you explain? everything works fine. the only problem i have is the same hash is being generated. <?php echo md5( strtolower( trim( email ) ) ); ?> That's because you're working on the string 'email', not the value email. PHP variables are named with $, and you won't be able to access JavaScript variables in the manner you showed without using AJAX to communicate between the browser and the server. If you run your code with warnings on, you'll get this message: Notice: Use of undefined constant email - assumed 'email' in - on line ... This is why the hash is the same at all times - your code is actually doing: <?php echo md5( strtolower( trim( 'email' ) ) ); ?>
I ended up just using a JS equivalent MD5 like this: var MD5=function(s){function L(k,d){return(k<<d)|(k>>>(32-d))}function K(G,k){var I,d,F,H,x;F=(G&2147483648);H=(k&2147483648);I=(G&1073741824);d=(k&1073741824);x=(G&1073741823)+(k&1073741823);if(I&d){return(x^2147483648^F^H)}if(I|d){if(x&1073741824){return(x^3221225472^F^H)}else{return(x^1073741824^F^H)}}else{return(x^F^H)}}function r(d,F,k){return(d&F)|((~d)&k)}function q(d,F,k){return(d&k)|(F&(~k))}function p(d,F,k){return(d^F^k)}function n(d,F,k){return(F^(d|(~k)))}function u(G,F,aa,Z,k,H,I){G=K(G,K(K(r(F,aa,Z),k),I));return K(L(G,H),F)}function f(G,F,aa,Z,k,H,I){G=K(G,K(K(q(F,aa,Z),k),I));return K(L(G,H),F)}function D(G,F,aa,Z,k,H,I){G=K(G,K(K(p(F,aa,Z),k),I));return K(L(G,H),F)}function t(G,F,aa,Z,k,H,I){G=K(G,K(K(n(F,aa,Z),k),I));return K(L(G,H),F)}function e(G){var Z;var F=G.length;var x=F+8;var k=(x-(x%64))/64;var I=(k+1)*16;var aa=Array(I-1);var d=0;var H=0;while(H<F){Z=(H-(H%4))/4;d=(H%4)*8;aa[Z]=(aa[Z]|(G.charCodeAt(H)<<d));H++}Z=(H-(H%4))/4;d=(H%4)*8;aa[Z]=aa[Z]|(128<<d);aa[I-2]=F<<3;aa[I-1]=F>>>29;return aa}function B(x){var k="",F="",G,d;for(d=0;d<=3;d++){G=(x>>>(d*8))&255;F="0"+G.toString(16);k=k+F.substr(F.length-2,2)}return k}function J(k){k=k.replace(/rn/g,"n");var d="";for(var F=0;F<k.length;F++){var x=k.charCodeAt(F);if(x<128){d+=String.fromCharCode(x)}else{if((x>127)&&(x<2048)){d+=String.fromCharCode((x>>6)|192);d+=String.fromCharCode((x&63)|128)}else{d+=String.fromCharCode((x>>12)|224);d+=String.fromCharCode(((x>>6)&63)|128);d+=String.fromCharCode((x&63)|128)}}}return d}var C=Array();var P,h,E,v,g,Y,X,W,V;var S=7,Q=12,N=17,M=22;var A=5,z=9,y=14,w=20;var o=4,m=11,l=16,j=23;var U=6,T=10,R=15,O=21;s=J(s);C=e(s);Y=1732584193;X=4023233417;W=2562383102;V=271733878;for(P=0;P<C.length;P+=16){h=Y;E=X;v=W;g=V;Y=u(Y,X,W,V,C[P+0],S,3614090360);V=u(V,Y,X,W,C[P+1],Q,3905402710);W=u(W,V,Y,X,C[P+2],N,606105819);X=u(X,W,V,Y,C[P+3],M,3250441966);Y=u(Y,X,W,V,C[P+4],S,4118548399);V=u(V,Y,X,W,C[P+5],Q,1200080426);W=u(W,V,Y,X,C[P+6],N,2821735955);X=u(X,W,V,Y,C[P+7],M,4249261313);Y=u(Y,X,W,V,C[P+8],S,1770035416);V=u(V,Y,X,W,C[P+9],Q,2336552879);W=u(W,V,Y,X,C[P+10],N,4294925233);X=u(X,W,V,Y,C[P+11],M,2304563134);Y=u(Y,X,W,V,C[P+12],S,1804603682);V=u(V,Y,X,W,C[P+13],Q,4254626195);W=u(W,V,Y,X,C[P+14],N,2792965006);X=u(X,W,V,Y,C[P+15],M,1236535329);Y=f(Y,X,W,V,C[P+1],A,4129170786);V=f(V,Y,X,W,C[P+6],z,3225465664);W=f(W,V,Y,X,C[P+11],y,643717713);X=f(X,W,V,Y,C[P+0],w,3921069994);Y=f(Y,X,W,V,C[P+5],A,3593408605);V=f(V,Y,X,W,C[P+10],z,38016083);W=f(W,V,Y,X,C[P+15],y,3634488961);X=f(X,W,V,Y,C[P+4],w,3889429448);Y=f(Y,X,W,V,C[P+9],A,568446438);V=f(V,Y,X,W,C[P+14],z,3275163606);W=f(W,V,Y,X,C[P+3],y,4107603335);X=f(X,W,V,Y,C[P+8],w,1163531501);Y=f(Y,X,W,V,C[P+13],A,2850285829);V=f(V,Y,X,W,C[P+2],z,4243563512);W=f(W,V,Y,X,C[P+7],y,1735328473);X=f(X,W,V,Y,C[P+12],w,2368359562);Y=D(Y,X,W,V,C[P+5],o,4294588738);V=D(V,Y,X,W,C[P+8],m,2272392833);W=D(W,V,Y,X,C[P+11],l,1839030562);X=D(X,W,V,Y,C[P+14],j,4259657740);Y=D(Y,X,W,V,C[P+1],o,2763975236);V=D(V,Y,X,W,C[P+4],m,1272893353);W=D(W,V,Y,X,C[P+7],l,4139469664);X=D(X,W,V,Y,C[P+10],j,3200236656);Y=D(Y,X,W,V,C[P+13],o,681279174);V=D(V,Y,X,W,C[P+0],m,3936430074);W=D(W,V,Y,X,C[P+3],l,3572445317);X=D(X,W,V,Y,C[P+6],j,76029189);Y=D(Y,X,W,V,C[P+9],o,3654602809);V=D(V,Y,X,W,C[P+12],m,3873151461);W=D(W,V,Y,X,C[P+15],l,530742520);X=D(X,W,V,Y,C[P+2],j,3299628645);Y=t(Y,X,W,V,C[P+0],U,4096336452);V=t(V,Y,X,W,C[P+7],T,1126891415);W=t(W,V,Y,X,C[P+14],R,2878612391);X=t(X,W,V,Y,C[P+5],O,4237533241);Y=t(Y,X,W,V,C[P+12],U,1700485571);V=t(V,Y,X,W,C[P+3],T,2399980690);W=t(W,V,Y,X,C[P+10],R,4293915773);X=t(X,W,V,Y,C[P+1],O,2240044497);Y=t(Y,X,W,V,C[P+8],U,1873313359);V=t(V,Y,X,W,C[P+15],T,4264355552);W=t(W,V,Y,X,C[P+6],R,2734768916);X=t(X,W,V,Y,C[P+13],O,1309151649);Y=t(Y,X,W,V,C[P+4],U,4149444226);V=t(V,Y,X,W,C[P+11],T,3174756917);W=t(W,V,Y,X,C[P+2],R,718787259);X=t(X,W,V,Y,C[P+9],O,3951481745);Y=K(Y,h);X=K(X,E);W=K(W,v);V=K(V,g)}var i=B(Y)+B(X)+B(W)+B(V);return i.toLowerCase()}; and then I set the hash var to this var hash = MD5(email.trim().toLowerCase()); This seems to have worked.
jQuery reading JSON Data
So I have a database pass a whole bunch of data using PHP back to jQuery through JSON.. I'm trying to access individual columns from the returned data but no luck.. My PHP: header('Content-Type: application/json'); require 'database.php'; mysql_query('SET CHARACTER SET utf8'); $myjsons = array(); $qry = 'SELECT * FROM quotes ORDER BY id'; $result = mysql_query($qry); while($row = mysql_fetch_assoc($result)){ $myjsons[] = json_encode(array($row)); } echo $_GET['callback'] . '(' . json_encode($myjsons) . ')'; Here's my JS: function getAll(){ jQuery.ajax({ url:'example.com/js/getAll.php', async: true, dataType: 'jsonp', success:function(data){ $('.quoteList').append(data[0]); } }); } Currently that appends the following to the element: [{"id":"1","text":"The most wasted of all days is one without laughter.","author":"E.E. Cummings","status":"1"}] So for example.. if I wanted the jQuery to go through data[0] to data[92] (the last one) and append the author of each to .quoteList, how could I do that? I've tried data[0][1] and data[0][author]
You can use $.each() to loop through the data and append the author: $.each(data, function(i) { $('.quoteList').append(data[i]['author']); });
The PHP might be defective because json_encode is called twice, and this is unusual. As written this would be flattening the rows into JSON strings, but mere strings nonetheless, which then get JSON encoded again into an array of strings. This is probably not what you intended, as it would be making it possible to print the received data but not access the components of rows which will be decoded to strings and not objects. Compare https://stackoverflow.com/a/6809069/103081 -- here the PHP echoes back a callback with a single JSON object inside parenthesis (). I suspect the fix looks like https://stackoverflow.com/a/15511447/103081 and can be adapted as follows: header('Content-Type: application/json'); require 'database.php'; mysql_query('SET CHARACTER SET utf8'); $myjsons = array(); $qry = 'SELECT * FROM quotes ORDER BY id'; $result = mysql_query($qry); while($row = mysql_fetch_assoc($result)){ $myjsons[] = $row; } echo $_GET['callback'] . '(' . json_encode($myjsons) . ')'; Once you have this, you should be getting back proper JSON for your array of objects and be able to use #Felix's code on the client side.
you need to use loop on your data, try this success:function(data){ for (var i in data) { $('.quoteList').append(data[i]); } }
This should work: (upd. all code:) function getAll(){ jQuery.ajax({ url:'example.com/js/getAll.php', async: true, dataType: 'jsonp', contentType: "application/json", success:function(data){ var str = ""; $(data).each(function(index, item){ str += item.author + " "; }); $('.quoteList').append(str); } }); }
Your problem is here: while($row = mysql_fetch_assoc($result)){ $myjsons[] = json_encode(array($row)); } echo $_GET['callback'] . '(' . json_encode($myjsons) . ')'; you need something like this: while($row = mysql_fetch_assoc($result)){ $myjsons[] = $row; } $myjsons['callback'] = $_GET['callback']; echo json_encode($myjsons);
Keep GET-Values saved
I have function like this: function SetPageShow (obj) { window.location.href="?CMD=PAGEROWS&PARA="+obj.options[obj.selectedIndex].text; } and it works fine until I have a page with another GET-values like http://protectneu/main.php?site=mitarb&liz=260 . then when I call the function SetPageShow, the URL will be http://protectneu/main.php?CMD=PAGEROWS&PARA=25 and the other values(mitarb and liz) are getting lost. Is there a way to keep them saved and just add the new paramethers. The result that I need is: http://protectneu/main.php?site=mitarb&liz=260&CMD=PAGEROWS&PARA=25
if (window.location.search) return window.location.href + "&CMD=PAGEROWS&PARA="+obj.options[obj.selectedIndex].text; else return window.location.href + "?CMD=PAGEROWS&PARA="+obj.options[obj.selectedIndex].text;
On the PHP side you need to strip out the parameters that you would send via JavaScript, then build a string with the other parameters, like so: <?php $params = $_GET; unset($params['CMD']); unset($params['PARA']); ?> <script> function SetPageShow(obj) { var params = '<?php echo http_build_query($params); ?>'; window.location.href = '?' + (params ? params + '&' : '') + "CMD=PAGEROWS&PARA=" + encodeURIComponent(obj.options[obj.selectedIndex].text); } Btw, I've also added encodeURIComponent() in JavaScript to perform proper escaping of the selected value.
Consider using PHP's sessions if you want to retain information like this. <?php session_start(); $_SESSION['foo'] = $bar; ?> Then you can refer to this information on other pages by calling session_start() at the beginning of the page. <?php session_start(); $bar = $_SESSION['foo']; ?>