Jump to content

wisconsinWildcat

Members
  • Content Count

    14
  • Joined

  • Last visited

Community Reputation

0 Neutral

About wisconsinWildcat

  • Rank
    Member
  1. Here's what worked for me. First, the calling event (this one happens to be triggered by page load): function loadRolesTitleSelection() { $('#rolesTitle').find('option').remove().end(); $('#rolesLocation').find('option').remove().end(); $('#rolesTag').find('option').remove().end(); $('#rolesTagCollection').empty(); $( '#rolesCalcOutput').empty(); console.log('loadRolesTitleSelection entered.'); $.ajax({ type: 'POST', url: '../../../components/json_getAllRoles.php', dataType: 'JSON', success: function(data) { var $optList = $('#rolesTitle'); $optList.empty(); $optList.append('<option selected="true" disabled>Choose Job</option>'); $optList.prop('selectedIndex',0); $.each(data, function(i,val){ $optList.append('<option value="'+data[i]['role']+'">'+data[i]['role']+'</option>'); }); }, error: function(xhr, textStatus,errorThrown){ console.log('Error in g_primary.js.loadRolesTitleSelection.',errorThrown); console.log(xhr); alert(errorThrown); } }); $( '#rolesLocation' ).prop("disabled", true); $( '#rolesTag' ).prop("disabled", true); console.log('loadRolesTitleSelection exiting.'); } and here is json_getAllRoles.php (connect_database.php is simply a mysqli database connection include file): <?php header('Content-Type: application/json'); include_once( "connect_database.php" ); $sql = "SELECT role FROM pkdb.uniqueroles ORDER BY role"; $json = array(); $result = mysqli_query( $dbhandle, $sql ); while($row = mysqli_fetch_array($result)) { $temp = array( 'role' => $row["role"] ); array_push( $json, $temp ); } $jsonstring = json_encode($json); echo $jsonstring; ?>
  2. I apologize for the delay in responding. Life threw me a couple curveballs I had to contend with. Maxxd, I appreciate your explanation more than you know. I've been in the computer field and programming since 1978 and I've forgotten more languages (most self-taught, many in the mainframe realm, then PC's when they came out: MAPPER, SSG, Basic, Pascal, Fortran, COBOL, Assembler, PowerBuilder, dBASE, Foxpro, and on and on) than most programmers ever learn, but I never did quite get the ajax stuff, and I'm brand new to PHP. Your explanation helps me a great deal, and I feel it's going to help more folks who stumble upon this thread. Barand, I have an include file for my connection, and I include that file before every database action. Perhaps it's not correct for PHP, but in past programs I've learned to re-use programs and minimize the amount of code I ever have to re-write for new systems. I'd bet you do as well. I'm sorry I didn't include the entire file, but I am trying to protect my client. taquitosensei, Thanks for chiming in. One day, this will all click, like it's done for so many programs before, and I'll be off and running. I certainly appreciate all the help from all of you! Thank you! I'm not "there" yet, but I hope to be soon. May you all enjoy 41+ years of programming! And I'm nowhere near done yet. PHP / Ajax / MySQL is just the next thing for me.
  3. I followed answer 2 on https://stackoverflow.com/questions/17356469/how-to-call-a-class-function-from-jquery-php and now do not get any errors, but it doesn't stuff the result back to the form. That's a different problem. I'll keep grinding. Thanks for the help! P.S. What I don't understand in that method is why I can't have it all in one file. My main page calls a js file that calls a single-use php file that instantiates an object from yet another file. Seems like over-complication.
  4. ...and are you recommending jobsDatabase(0,0,0,0); because there are four PUBLIC statements at the beginning, and this provides placeholders for them?
  5. I'm sorry, but add it to the end of which file? I can't add it after the Ajax command because that's where the <option> statement is assembled.
  6. yes, I wrote it myself. Right now, what I shared here is the entire thing, to start. It will grow much larger with functions that accept inputs as I do the conversion, but I wanted this first one to work without pollution from other functions getting in the way.
  7. Sadly, "/jobsDatabase.php?method=getAllRoles" shows nothing. I changed it to "echo $jsonstring;" and still nothing. Here's what Headers show when I call it with url/script?method (I think I changed just enough to protect my client and my workstation) which makes it look like I'm making the trip there successfully, but it results in no data output: Request URL: http://domain-name-here/components/jobsDatabase.php?method=getAllRoles Request Method: GET Status Code: 200 OK Remote Address: xxx.xxx.200.71:80 Referrer Policy: no-referrer-when-downgrade Response Headersview source Content-Length: 0 Content-Type: text/html Date: Mon, 18 Mar 2019 19:27:19 GMT Server: Microsoft-IIS/7.5 X-Powered-By: ASP.NET X-Powered-By-Plesk: PleskWin Request Headersview source Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.9 Cache-Control: max-age=0 Connection: keep-alive Cookie: PHPSESSID=7hg9abcdefg8ii5rjtne1chph3g1 Host: domain-name-here Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36 Query String Parametersview sourceview URL encoded method: getAllRoles
  8. Ah, I had the dev tools up and the Network tab opened, but didn't know what you wanted to see from there. Great to know and I can there to see the response, but.... "This request has no response data available."
  9. Would I be able to replace the ajax call with $job_data = new jobsDatabasee(); $data = $job_data->getAllRoles(); die($data); Admittedly, this is an area of jQuery/Javascript and PHP that is uncharted territory for me.
  10. I'm not used to using the Network tab - didn't need it with ColdFusion. What am I looking for there? I do get the error "SyntaxError: Unexpected end of JSON input" popping up from the ajax error output though.
  11. maxxd, isn't the method= portion of the ajax command doing just that, calling for the execution of the getAllRoles function? Not sure how else I'd do an Ajax call without that. From what I ascertain about taquitosensei's post, I would replace the ajax command with the object call instead, right?
  12. Nice, thank you! Didn't know exactly how to do that in PHP, you may have just given me the tools to finish this conversion! wW
  13. "you are calling the jobsDatabase::getAllRoles() method, right?" Um...I definitely think so? 🙄 I thought the jQuery ajax code did that. I also include it. I posted everything pertinent. By virtue of you asking that question, though, you make me doubt that. How should I be making this event driven call?
  14. Newbie here. I have an ajax method in jquery calling a PHP class to search a database and return a single column in jSON, but I'm getting the "unexpected end" error message. It doesn't have to be jSON if there's an easier or more sure way. This is the meat of the jSON call. It was used for a ColdFusion component and was modified for PHP, so it's possible I left some chaff behind and don't recognize it. $.ajax({ url: '../../../components/jobsDatabase.php', method: 'POST', dataType: 'json', data: { method: 'getAllRoles', returnFormat: 'json' }, success: function(data) { var $optList = $('#rolesTitle'); console.log('Success retrieving data.',data); $optList.empty(); $optList.append('<option selected="true" disabled>Choose Job</option>'); $optList.prop('selectedIndex',0); $.each(data, function(i,val){ $optList.append('<option value="'+data[i]['role']+'">'+data[i]['role']+'</option>'); }); console.log('Done loading options.'); }, error: function(xhr, textStatus,errorThrown){ console.log(xhr); alert(errorThrown); } }); Here's the class: <?php class jobsDatabase { public $role; public $tag; public $location; public $job_id; function __construct( $role, $tag, $location, $job_id ) { $this->role = $role; $this->tag = $tag; $this->location = $location; $this->job_id = $job_id; } function getAllRoles() { include( "connect_database.php" ); $sql = "SELECT role FROM thedatabasenamehere.uniqueroles ORDER BY role"; $json = array(); $result = mysqli_query( $dbhandle, $sql ); mysqli_close($dbhandle); while($row = mysqli_fetch_assoc($result)) { $temp = array( 'role' => $row["role"] ); array_push( $json, $temp ); } $jsonstring = json_encode($json, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_FORCE_OBJECT); echo $jsonstring; die(); } } // End of the class jobsDatabase ?> I've run the connect-to-database and SQL/select separately and they work fine. I wish you could easily use console.log() in PHP, it would sure help with debugging! Yes, I use a real database name for "thedatabasenamehere". I started the encode with no options, been trying some things out of desperation. Also, shouldn't it "return $jsonstring" instead of echo it? All the online references I found used echo so I'm confused. Any help greatly appreciated. Thank you! WW
×
×
  • 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.