• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change the text on a button progamatically?

#11
Well I made it back. Turns out that the jquery code I put in the bottom of the view file was breaking everything. I finally removed that script and it started working again. I managed to add in the css-class make-active. But I can't seem to get the alert to popup. I wish I could set a breakpoint in the script just to see if it gets called. What happens is that someone presses the make active button and my company_update() function gets called. In it I do a bunch of database things. The last line is echo $campaign, which runs, but I have no idea where that goes.

I did try removing

$('.make-active').click(function(e){
   e.preventDefault();


from
<script>
   $(document).ready(function(){
       $('.make-active').click(function(e){
           e.preventDefault();
           alert('You clicked on a button with the css class make-active.');
       });
   });
</script>


The alert seems to work. So maybe it is the .click? I have no idea what function(e) is?
Reply

#12
I'm confused. Does the alert work or not?
If it works, it means your jQuery is ok.
The e in function(e) passes the event of the object you clicked. In this case, the event is running a url. That shouldn't happen, that's why e.preventDefault() will prevent it.

Next step is inserting the AJAX part in the script.
I've already given you an example. Use jQuery's $.post() method.
It must call a controller/method (in CI). You can pass values to it, as shown in the example.
The method must end with an echo statement. The echoed text will be returned as return value to your jQuery script. You can use that to update the danger button.

Good luck.
Reply

#13
>>I'm confused. Does the alert work or not?

The Jquery as written does not work. If I get rid of the onclick part (ie just the DOM has loaded) it alerts. But I can't seem to get the event from the

$crud->add_action('Make Active', '', 'Configure/company_update','make-active');
Reply

#14
I created a simple view to test it, and in my situation, it works:
Code:
<html>

<head>
   <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>

<body>
<h1>Test button</h1>

<div>
<button id="myCamp" class="btn pull-right btn-danger navbar-btn">Active Campaign: None</button>
</div>

<br />

<div>
<button class="make-active">Set My Campaign</button>
</div>

<script>
   $(document).ready(function(){
       $('.make-active').click(function(e){
           e.preventDefault();
           alert('You clicked on a button with the css class make-active.');
       });
   });
</script>

</body>
</html>
Reply

#15
Yes. I tried out your code and your code works. I guess the problem is that MY buttons are being created like this:

 $crud->add_action('Make Active', '', 'Configure/company_update','make-active');

the company update() sets the database up and zeros out the other actives (for this user). The 'make-active' is defined as a css_class. Is there more than one type of class? There is one button for each campaign. I did a screen shot. See the Set My Campaign button? That is your button. See the six Make Active buttons? Those are my buttons. So when a user clicks one of them, company_update() gets the $row and then I can get the campaign name (ie chrysler) by using the $row number. I just am not able to update the red Active Campaign label from my company_update() function.


Attached Files Thumbnail(s)
   
Reply

#16
How important is it that the red button is updated without refreshing the page?
If it isn't, simply let the the company_update method reload the view.
Pass the activated campaign in the $data array and use that to set the text of the button.

I think it will get very complicated if you want to do that with AJAX, because Grocery Crud doesn't give you full control over the way buttons are created.

If you need more help, please share your Configure controller. Or at least the method that initially displays the table with Campaigns, and the company_update method.
Reply

#17
    Well I got a solution! What I did was get rid of the red button and added this line  in my _configure_output()

public function _configure_output($output = null)
  {
      // $GLOBALS['title']="this is my title";
      //  $this->load->vars( array( 'html_title' => $this->session->userdata('campaign')) );     // <- This puts $html_title into the context for the view() below
      //set default campaign
       if ($output<>null) {
              echo "<br><br><h1>ACTIVE CAMPAIGN: " . $this->session->userdata('campaign') . "</h1>";
      }
     $this->load->view('configure.php',(array)$output);
}


I trial and error'd it into the right spot. Thank you so much for your help! 

The ONLY problem now is that when I first start up, I get two of these headers! See image. I guess this is because _configure_ouput gets called once (from somewhere) where $output: {output=> "", js_files=>[0], css_files=>[0]} [3]. For some reason i can't seem to catch ($output<>null). 
 

You have been fantastic, btw.
Reply

#18
I'm not familiar with CodeIgniter Simplicity, so I'm not sure where it's going wrong.
If you choose to store the active campaign in a session variable, you can stick with the old code you had, and set the text of the red button accordingly.

PHP Code:
<button id="myCamp" class="btn pull-right btn-danger navbar-btn">Active Campaign: <?= $this->session->campaign;?></button> 

If you don't want to use a button, you should use css to style the element. One way is by using class names (like Bootstrap is doing for a lot of elements). Another way is setting the style attribute of the element.

Example:
PHP Code:
<div style="background-color: black; color: white; padding: 12px; font-size: 20pt; font-weight: bolder;">
<?= 
$this->session->campaign;?>
</div> 

A good tutorial about css: https://www.w3schools.com/css/
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.