Jump to content

Barand

Moderators
  • Posts

    24,343
  • Joined

  • Last visited

  • Days Won

    795

Everything posted by Barand

  1. Yes. Here's a simplified version of your application as an example FORM CODE <?php // // FOR DEBUG PURPOSES ONLY - LIST CONTENTS OF SESSION PLAYLIST // session_start(); if (isset($_SESSION['playlist'])) { echo '<pre>', print_r($_SESSION['playlist'], 1), '</pre>'; echo "</hr><br>\n"; } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $().ready( function() { $(".btn").click( function() { var vid = $(this).data("id"); var vname = $(this).data("name"); $.post( "my_cart.php", { "voice_id" : vid, "voice_name" : vname}, function(resp) { var list = "<tr><td><b>ID</b></td><td><b>Title</b></td></tr>\n"; $.each(resp, function(k, v) { list = list + "<tr><td>" + k + "</td><td>" + v + "</td></tr>\n" }) $("#playlist").html(list) }, "JSON" ) }) }) </script> </head> <body> Song 1 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="1" data-name="song-1.mp3">Add to PlayList </button> <br> Song 2 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="2" data-name="song-2.mp3">Add to PlayList </button> <br> Song 3 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="3" data-name="song-3.mp3">Add to PlayList </button> <br> Song 4 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="4" data-name="song-4.mp3">Add to PlayList </button> <br> Song 5 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="5" data-name="song-5.mp3">Add to PlayList </button> <br> Song 6 <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="6" data-name="song-6.mp3">Add to PlayList </button> <br> <br> <h2>Playlist</h2> <table style="width:600px" id="playlist"> </table> </body> </html> MY_CART.PHP <?php session_start(); if ($_SERVER['REQUEST_METHOD']=='POST') { $voice_id = $_POST['voice_id'] ?? 0; $voice_name = $_POST['voice_name'] ?? ''; if ($voice_id && $voice_name) { $_SESSION['playlist'][$voice_id] = $voice_name; exit(json_encode($_SESSION['playlist'])) ; } } exit("ERROR") ; ?>
  2. my_cart.php will not display anything to the user. When you use ajax, all output is sent back to the calling ajax function in the ajax response. In your case it will be in the "data" argument success: function(data) // <-- output from "my_cart.php" is in "data" { console.log(data); // console.log('success',data); } Note that these are processed one at a time as you click each button. If you want to list multiple items then you would add them to a list on the calling page, not in my_cart.php. Alternatively you could store them with my_cart.php (maybe in a "playlist" table in a database) and list them in another page. If the list needs to be persistent, store them.
  3. Yes, it does. I can see it quite clearly (indicated by ^ ). But it shouldn't have.
  4. Use the developer tools in your browser (network tab) and check the ajax message and response. Have you changed the html in your form so you now have voice_id and voice_name values?
  5. According to the XML provided, assigned staff only have a id and name. Where are the other data coming from? <Assigned> <Staff> <ID>610781</ID> <Name>Staff 1</Name> </Staff>
  6. Yet, there it is in the manual. The php.net function search is crap in this respect. You have to search first for the "mysqli-result" class to get to the methods fetch() is mysqli-statement method fetch_assoc() is a mysqli-result method. That's the main problem with mysqli - there are two different sets of methods to process the results depending on whether you used query() or prepare().
  7. When you say "midnight US", to which of your six timezones do you allude? It could mean any time between GMT 04:00 and GMT 10:00, (or, perhaps, you just mean "mañana" ?)
  8. On further thought you could forget my hidden inputs and just put the two values in the button as "data-id" and "data-name". Then, in the click function var voice_id = $(this).data("id") var voice_name = $(this).data("name")
  9. On closer examination, your button script is expecting HTML elements with ids of "voice_id" and "voice_name" with values. You don't have any such elements ids must be unique and you will have several buttons and voice_ids. i would add hidden fields to hold the names and ids <input type="hidden" name="voice_id" value="{$row[voice_id]}" class="voice_id" data-id="{$row[voice_id]}"> <input type="hidden" name="voice_name" value="{$row[voice_name]}" class="voice_name" data-id="{$row[voice_id]}"> <button type="button" class="btn btn-primary" type="submit" style="padding: 5px 83px 5px 83px;" data-id="{$row[voice_id]}">Add to PlayList </button> Note the hidden fields and button all share the same data-id values. The use those data-id values to get the voces associated with buttons $('.btn').on('click',function() { var vid = $(this).data("id") var voice_id = $(".voice_id[data-id="+vid+"]").val(); var voice_name = $(".voice_name[data-id="+vid+"]").val(); $.ajax({ type : "POST", url : "my_cart.php", datatype : "text", data : {voice_id: voice_id, voice_name: voice_name }, success: function(data) { console.log(data); // console.log('success',data); } }); });
  10. @john7911 Just for completeness... Where you have several case values which call the same the code you can do something like this instead of repeating the code block for each value <?php $diametre = $_GET['diametre'] ?? 0; switch ($diametre) { case 2: case 3: case 4: case 6: case 8: case 10: case 12: case 14: case 16: case 18: case 20: case 22: case 24: case 26: echo "do something"; break; default: echo "do something else"; } ?>
  11. I think this is what the ASP code was attempting to do FORM PAGE <?php define("HOST",'localhost'); // define("USERNAME",'****'); // define("PASSWORD",'****'); // define("DATABASE", 'test'); // This code would normally // be in an included file $db = new PDO("mysql:host=".HOST.";dbname=".DATABASE, USERNAME, PASSWORD); // to avoid repetition $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // on every page $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // function bulletinOptions($db) { $opts = "<option value=''>- select bulletin -</option>\n"; $result = $db->query("SELECT bulletinid , bulletindate FROM tblbulletins ORDER BY bulletinid "); foreach ($result as $r) { $opts .= "<option value='{$r['bulletinid']}'>{$r['bulletindate']}</option>\n"; } return $opts; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> </head> <body> <form method="GET" action="pg01.php"> <input type="hidden" name="mode" value="view"> <label>Bulletin: </label> <select name="ID"> <?=bulletinOptions($db)?> </select> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html> PG01.PHP <?php include 'db_inc.php'; // contains connection code $bulletin_date = "Invalid bulletin id"; $bulletin_text = ''; if (isset($_GET['ID'])) { $stmt = $db->prepare("SELECT DATE_FORMAT(bulletindate, '%m-%d-%Y') as date , bulletintext FROM tblbulletins WHERE bulletinid = ? "); $stmt->execute( [ $_GET['ID'] ] ); if ($row = $stmt->fetch() ) { $bulletin_date = $row['date']; $bulletin_text = $row['bulletintext']; } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> </head> <body> <h4><?=$bulletin_date?></h4> <p><?=$bulletin_text?> </body> </html>
  12. I am curious about what is "partially working". As far as I can see, none of it should. You create a mysqli connection but after that you use the now extinct mysql_xxx functions for your query and processing. Your only output is "echo $link" but $link does not appear to be defined anywhere in the code You will find it more advantageous in the long run to learn to use PDO instead of mysqli for your database access.
  13. This may not be what you you want but it should get you on your way <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Simplified Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> $().ready( function() { $("#testa").change( function() { var row = $(this).parent().parent() var nextrow = $(row).next() var id = $(nextrow).attr("id") if (id.indexOf('shipping')==0) { $(nextrow).remove() } }) }) </script> <style type="text/css"> table {border-collapse: collapse; width: 500px} td { padding: 16px; text-align: center} </style> </head> <body> <table border='1'> <tr> <td> <select name='testa' id='testa'> <option value=''>-?-</option> <option value='1'>1</option> <option value='2'>2</option> </select> </td> <td>123456</td> </tr> <tr id='shippingxxx'> <td>234567</td> <td>345678</td> </tr> <tr id='yyy'> <td>34567</td> <td>45678</td> </tr> </table> </body> </html>
  14. A table of infinite length? That must be memory-intensive. When you say the next row has an id=shippingXXX are you saying the <tr> element has that id or the row contains a <td> with that id (or even an element contained in a <td> has that id)? Sample HTML would assist, as would any code you have tried.
  15. The code in each switch is identical so all it achieves is to ensure the calculation uses only the defined list of diameter options. Just use an array of the valid values to verify the values. You can use the same array to generate the option list <?php $diam_vals = [2,3,4,6,8,10,12,14,16,18,20,22,24,26]; $results = ''; if ($_SERVER['REQUEST_METHOD']=='POST') { $x = $_POST['x'] ?? 0; $y = $_POST['y'] ?? 0; $diametre = $_POST['diametre'] ?? 0; if ($x > 0 && $y > 0 && in_array($diametre, $diam_vals)) { $rayon = $diametre * 38.1; $dc = $x/2; $ad = ($y/2)-$rayon; $ac = sqrt(pow($ad,2) + pow($dc,2)); $ec = sqrt(pow($ac,2) - pow($rayon,2)); $LongueurBayonette = $ec*2; $alpha = asin($dc/$ac); $alpha = $alpha*180/M_PI; $beta = acos($rayon/$ac); $beta = $beta*180/M_PI; $angle = 180-$alpha-$beta; $results .= "X = " . $x . "mm" . "<br/>"; $results .= "Y = " . $y . "mm" . "<br/>"; $results .= "Longueur = " . number_format($LongueurBayonette,1) . " mm" . "<br/>"; $results .= "&beta; = " . number_format($angle,1) . "°" . "<br/>"; $results .= "Rayon = " . $rayon . " mm" . "<br/>"; $results .= "&phi; = " . $diametre . '"' . "<br/>"; } else { $results = 'Inputs are not valid'; } } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Simplified Example</title> </head> <body> <form method="post" action=""> <fieldset> X: <input type="text" name="x" value="" /> <br/> Y: <input type="text" name="y" value="" /> <br/> Diametre: <select name="diametre"> <option value="0"> </option> <?php foreach ($diam_vals as $d) { echo "<option value='$d'>$d</option>\n" ; } ?> </select> <input type="submit" value = "Calculer" /> </fieldset> </form> <br> <?=$results?> Just curious - do you have a diagram of how those values relate to one another. It metions "rayon" and "bayonnette" so my guess is that it is some kind of laser rifle with attached bayonet (but I could be wrong) ?
  16. $pdo is defined here $pdo = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,$dbpassword); You have completely ignored comments re this code and left it in its incorrect state... $userexists = $pdo->prepare("SELECT COUNT(username) FROM users WHERE username= '$username' LIMIT 1"); $userexists->bindParam(':username', $username); BTW, the LIMIT 1 is redundant - that query will only ever return a single row with the record count.
  17. The piece of code indicated by gizmola also has other errors $userexists = $pdo->prepare("SELECT COUNT(username) FROM users WHERE username= '$username' LIMIT 1"); $userexists->bindParam(':username', $username); $userexists->execute(); You are attempting to bind the $username to the :username placeholder - but you haven't used a placeholder. Your PDO connection would look like this (and replace your mysqli connection) $pdo = new PDO("mysql:host=$servername;dbname=$dbname",$dbusername,$dbpassword); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); ... and your two mysqli queries would be replaced with $stmt = $pdo->prepare("SELECT contrasena , bloqueado FROM users WHERE username = :username "); $stmt->execute( ['username' => $username] ); $row = $stmt->fetch(); $realpass = $row['contrasena']; $bloqueado = $row['bloqueado'];
  18. Pity, because you need show that bit too. The first half of your code uses a mysqli connection and the latter half uses PDO. Do you have two connections? Any chance you could format your code into something legible next time and place it in a code box (<> button in toolbar)? A few line breaks and indents would be appreciated.
  19. You can't really call it "final" until it's been tested. With careful construction of your test data, avoiding string values, empty $criteria arrays and queries that return no records (still valid), it should be fine and let you sleep at night with the feeling you've done a good job.
  20. If you are constrained to three columns, I'd go with Date | Book:chapter:verse | Text and I'd put the data into a conventional CSV format that can be opened directly by Excel - EG "01/01/19","Lukas:16:19","Es war ein reicher Mann, der kleidete sich in Purpur und kostbares Leinen" "01/02/19","Kolosser:3:13","Ertrage einer den andern und vergebt euch untereinander" "01/03/19","1.Petrus:5:10","Der Gott aller Gnade, der euch berufen"
  21. That would require us knowing where you want to go. We can help you more if you explain exactly what this process should accomplish and what your database tables look like.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.