phppup Posted February 10, 2021 Share Posted February 10, 2021 I'm new to AJAX, and I'm working with some 3rd party code as a learning tool. Came across this section but: Quote $.ajax({ type: "POST", url: "my_code.php", data: "username="+ usr, success: function(msg){ I would prefer NOT having a my_code.php file, but rather to include the PHP code as part of the single file that already contains all the other scripting. Can this be achieved? What do I need to put in the URL line to direct it to self-examine? Pros and cons of proceeding this way? Thanks folks. Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/ Share on other sites More sharing options...
requinix Posted February 10, 2021 Share Posted February 10, 2021 Whatever code is in my_code.php has to be moved into the current file. Obviously. What happens after that depends on what the rest of the code looks like. If the file doesn't process POST data for anything else then you can use standard practices for handling form inputs (such as by checking if the REQUEST_METHOD is POST) and you don't have to change the AJAX any further. Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584360 Share on other sites More sharing options...
phppup Posted February 10, 2021 Author Share Posted February 10, 2021 I'll try that. But do I remove the Quote // url: "my_code.php", Or redirect it with a $this (of some sort? Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584361 Share on other sites More sharing options...
requinix Posted February 10, 2021 Share Posted February 10, 2021 You still need to tell the AJAX what URL to send the data to. Don't do any redirects. Change the "url" to be whatever page it is you want to reuse. Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584362 Share on other sites More sharing options...
phppup Posted February 10, 2021 Author Share Posted February 10, 2021 Ahhhh, now it's starting to make sense (I think). So either way, that piece of code needs to exist (if not too send, then to receive)? As a tangent, given two scripts to accomplish the same task, is AJAX any better or worse than a JSON ? Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584363 Share on other sites More sharing options...
Barand Posted February 10, 2021 Share Posted February 10, 2021 41 minutes ago, phppup said: is AJAX any better or worse than a JSON ? That's like asking if a radio transmitter is better than morse code. AJAX is method of communincation between client (javascript) and the server. JSON is a data format. Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584364 Share on other sites More sharing options...
phppup Posted February 11, 2021 Author Share Posted February 11, 2021 Day 2: making progress thanks to all I've learned here, but could REALLY use a link that would explain (in simplified terms) how to use AJAX in creating username availability. Thanks everyone. Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584386 Share on other sites More sharing options...
Barand Posted February 11, 2021 Share Posted February 11, 2021 Example of AJAX call to same page Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584387 Share on other sites More sharing options...
phppup Posted February 11, 2021 Author Share Posted February 11, 2021 (edited) Thanks. Any other basic tutorial links would be helpful too. While developing my script, I noticed that the username is NOT case sensative. (There is NO string-to-lowercase in my code, yet it is acting as if it exists) Did I miss a step in my database or table? Should username disallow case differences (which would limit the number of potential combinations)? Passwords come to mind too, of course. Edited February 11, 2021 by phppup Forgot item Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584391 Share on other sites More sharing options...
Barand Posted February 11, 2021 Share Posted February 11, 2021 In a database the case sensitivity of a field depends on which collation you apply to that field. If the collation name ends with "ci" it is case insensitive. The default collation would normally be case-insensitive but changed for fields where you specifically need sensitivity. I personally would count "fred" and "frEd" as the same username. You wouldn't be storing passwords as plain text for searching anyway. Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584394 Share on other sites More sharing options...
phppup Posted February 11, 2021 Author Share Posted February 11, 2021 Got it. (should names be case sensative) But AJAX is client side, so sanitizing is still highly recommended within PHP for the server side, right? On a related note, I've seen sanitizing examples that trim or remove HTML characters. Shouldn't an entry with such problems simply be rejected rather than repaired? Are the PHP sanitizing filters reliable on their own (to replace all the code that was required in the past)? Quote Link to comment https://forums.phpfreaks.com/topic/312125-ajax-with-php-on-same-page/#findComment-1584398 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.