Jump to content

code firing when I reload page?!?!?


Red2034

Recommended Posts

why is my INSERT code firing when I reload the page?

 

I put it in an <a href tag hoping it would only fire if I clicked the btn???

<a href="index.php" onclick"<?php $data['title'] = $row['title'];
 $data['enroll_date'] = date("Y-m-d");
$this->db->insert('class', $data);?>">

 <?php echo get_phrase('enroll');?>

</a>
Link to comment
Share on other sites

First off - thanks for the help, so i have made a php function with the  previous code:

function classes($param1 = '', $param2 = '')
    {      
       if ($param1 =='enroll') {
          $data['title'] = $row['title'];
          $data['deep_link'] = $row['deep_link'];
          $data['delivery_method'] = $row['delivery_method'];
          $data['status'] = 'not attempted';
          $data['description'] = $row['description'];
          $data['enroll_date'] = date("Y-m-d");
          $this->db->insert('class', $data);
           }...

and then edited the <a href...

<a href="#" class="btn btn-default btn-small" onclick="classes($param1 = 'enroll');"  >
                                    <?php echo get_phrase('enroll');?>
                                    </a>

hmmm - but still not working...

 

I guess I'll google 'how to call a php function with an HREF now

Link to comment
Share on other sites

you need to have the link make an http request to the php script when it's clicked. Whether you put it as the target href (passing relevant values as query params) or use javascript to make an ajax call to the php script depends on what your goal is. The former will either reload the whole page or else open a new window. The latter will stay on the same page without a reload.

 

But in either case, you can't directly call a php function from your link. It has to make a request to a script and you have to invoke the php function within the php script.

Link to comment
Share on other sites

http is a stateless protocol, so there is no "direct calls to server-side functions", not even with .NET. It's just that .NET abstracts all that away and makes it seem like you're doing it directly. Under the hood it's still stateless and does it with ajax.

 

IOW when .NET parses, it sees your server-side declared function and basically outputs a client-side javascript wrapper so when you make a call to your server-side function, what's really going on is you are making a call to a client-side javascript wrapper that makes an ajax call to the server and receives a response. So while it seems like you can use .NET to put a server-side function directly into an onclick, there's actually a middle-man under the hood, passing info back and forth. Which you're right, it's convenient. But there's no such thing built into php. But it's easy enough to replicate, and I'm sure there's a library or framework out there that does exactly that (I personally haven't looked)

Link to comment
Share on other sites

ok - so i think im close:

<?php function enroll() {
      if($_POST['action'] == 'call_this')
    {
          $data['title'] = $row['title'];
          $this->db->insert('class', $data);
          redirect(base_url() . 'index.php?student/classes/', 'refresh');
           }}
?>
<script language="javascript">
function fireEnroll() {
    
      $.ajax({
           type: "POST",
           url: "base_url() . 'index.php?student/classes/'",
           data:{action:'call_this'},
           success:function(html) {
             alert(html);
           }

      });
 }
 </script>

html

<a href="" class="btn btn-default btn-small" onclick="fireEnroll()"  >
                                    <?php echo get_phrase('enroll');?>
                                    </a>

this is firing the js func which posts to the php and does the redirect as the last call of the php, but it does not add the data to the table?!?!?

 

seems like a cludggy was to go about this...

Link to comment
Share on other sites

yes, you are on the right track.

 

One thing I see wrong is that in your $.ajax call you are again attempting to use a php function base_url() in a javascript (client-side) context. You need to wrap that in php tags like so:

 

url: "<?php echo base_url().'index.php?student/classes/'; ?>",
You can do this in this instance because you're making a call and echoing out the result on initial response. Though..sidenote.. I don't see anywhere where you are actually defining that php function.

 

 

And yes.. it is kinda "cludggy" but that's how it is - even with .NET - it's just that .NET abstracts all this, puts it "under the hood". It outputs code similar to this in principle just the same. The difference is that it does it for you, vs. you having to do it yourself with php and other server-side languages.

Link to comment
Share on other sites

In addition to that.. it's looking like you probably have issues with scope.

 

<?php function enroll() {
      if($_POST['action'] == 'call_this')
    {
          $data['title'] = $row['title'];
          $this->db->insert('class', $data);
          redirect(base_url() . 'index.php?student/classes/', 'refresh');
           }}
?>
your function doesn't know anything about $row. You need to expose it to your function. You either need to either declare it as a global variable first (bad idea) or expose it as a property for $this (or expose it via a getter method). Probably same thing to be said about $data. *technically* you can create $data['title'] on the fly, but I suspect you probably already have an existing array $data and adding an element to it, so you'll need to expose $data to your function as well, if that's the case
Link to comment
Share on other sites

<?php function enroll() {
     $classes = $this->db->get('class')->result_array();
            if($_POST['action'] == 'call_this')
    {
          $data['title'] = $row['title'];
          $this->db->insert('class', $data);
          
           }}
?>
<script language="javascript">
function fireEnroll() {
    
      $.ajax({ url: '<?php echo base_url().'index.php?student/classes/'; ?>',
         data: {action: 'call_this'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});
 }
 </script>


...
<?php $classesMaster = $this->db->get('class_master')->result_array();
                        foreach($classesMaster as $row):
                        ?>
 
                        <tr>
                         
                            <td width="50" align="center">
                                    <a href="" class="btn btn-default btn-small" onclick="fireEnroll()"  >
                                    <?php echo get_phrase('enroll');?>
                                    </a>
                            </td>
...

tried this to 'expose $row' to what I want but still no worky....

Link to comment
Share on other sites

thanks everyone for the help so far! I amlost have this working and because of the help this newbe understands php that much better...

that said, here my last issue:

 

title is coming up NULL - i guess I am not defining $classes correctly.... Can anyone explain this better or help me with this code below? I am trying to insert the title of only the row that an 'enroll' btn is associated with...

 if ($param1 == 'enroll') {
         $classes = $this->db->get_where('class_master', array('title'))->row();
          $data['title'] = $classes['title'];
          $this->db->insert('class', $data);
          redirect(base_url() . 'index.php?student/classes/', 'refresh');
           }
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.