Skip to Content Skip to Menu

🎃 Happy Halloween! Treat yourself with an awesome discount on memberships! Get 20% off now with code SPOOKY-2024!

[SOLVED] AJAX in CB tab plugins?

  • LnG
  • LnG
  • OFFLINE
  • Posts: 37
  • Thanks: 2
  • Karma: 0
9 years 3 months ago - 9 years 3 months ago #267382 by LnG
I am making a plugin that creates a tab with a link that exectutes a php file using AJAX. However, it will not execute the php file when clicking on the link. I have tried for 20 hours to get it working but somehow it just wont execute. Is cb blocking ajax in tabs? Am I missing something? Im soon going crazy.
Code:
$return .= '<script>function performAjaxSubmission() { $.ajax({ url: \''.JURI::base().'path/to/file/file.php\', method: \'POST\', data: { action: \'save\', field: $("#db_field").val(), val: $("#db_value").attr() }, dataType:\'json\', success: function(data) { alert(data.success); } }); return false; } jQuery(document).ready(function() { $(".savebtn").click(performAjaxSubmission); });</script>'; $return .= '<a href="#" class="savebtn" db_field="1" db_value="80">Click here</a>';
I put the javascript like that to see that the path to the php file was correct.
Code:
<?php $dbhost = 'localhost'; $dbuser = '*'; $dbpass = '*'; $dbname = '*'; $message = ''; try { $db = new PDO('mysql:dbname='.$dbname.';host='.$dbhost.';charset=utf8', ''.$dbuser.'', ''.$dbpass.''); } catch (PDOException $ex) { echo 'Connection failed: ' . $ex->getMessage(); } $userid = $_POST['db_field']; $level = $_POST['db_value']; $db->query('UPDATE `dbname` SET test="1" WHERE id="80"'); $message ="Updated Sucessfuly!"; echo json_encode(array('success'=>$message)); ?>

The query is also just a placeholder to see that its working, it is executing when I open the php file manually. I really need to get this working.
Thanks in advance,
LnG
Last edit: 9 years 3 months ago by krileon.

Please Log in or Create an account to join the conversation.

  • LnG
  • LnG
  • OFFLINE
  • Posts: 37
  • Thanks: 2
  • Karma: 0
9 years 3 months ago - 9 years 3 months ago #267383 by LnG
Replied by LnG on topic AJAX in CB tab plugins?
I just solved it with another code, still dont know what was wrong with the first one though.
A moderator can close or delete this thread, thanks.
Code:
<script>$(function(){ // Bind a click event to your anchor with id `updateSal` $("#updateSal").click(function(){ // get your employeeID var empID = "32"; // issue an AJAX request with HTTP post to your server side page. //Here I used an aspx page in which the update login is written $.post("'.JURI::base().'path/to/file/file.php", { EmpID: empID}, function(data){ // callack function gets executed alert("Return data" + data); }); // to prevent the default action return false; }); });</script>
Last edit: 9 years 3 months ago by LnG.

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48477
  • Thanks: 8281
  • Karma: 1443
9 years 3 months ago #267416 by krileon
Replied by krileon on topic AJAX in CB tab plugins?
CB does not block ajax calls. We can not advise you on custom coding, sorry. The correct way to utilize jQuery is to load it through Joomla or CB API. Echoing a script does not correctly prepare your jQuery, especially for noConflict usage. To output jQuery with CB you'd supply your JS string to the below function (excluding the script HTML).

Code:
$_CB_framework->outputCbJQuery( $js );

The above handles the noConflict, namespace, document ready, and loading of jQuery automatically.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • LnG
  • LnG
  • OFFLINE
  • Posts: 37
  • Thanks: 2
  • Karma: 0
9 years 3 months ago #267425 by LnG
Replied by LnG on topic AJAX in CB tab plugins?
This helped me alot actually, thank you for the information. Im very new to coding and though that the problem was with cb, but it was not. Thanks.

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum