Red2034 Posted January 31, 2014 Share Posted January 31, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/ Share on other sites More sharing options...
jcbones Posted January 31, 2014 Share Posted January 31, 2014 PHP is server side, and doesn't understand HTML, CSS, Javascript, or any other scripting language for that matter. It only understands PHP, but will receive info from HTTP, XML, JSON, etc... Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467249 Share on other sites More sharing options...
Red2034 Posted January 31, 2014 Author Share Posted January 31, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467254 Share on other sites More sharing options...
.josh Posted January 31, 2014 Share Posted January 31, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467256 Share on other sites More sharing options...
tdopun Posted January 31, 2014 Share Posted January 31, 2014 I'm more of a .NET guy recently cutting my teeth on PHP.. but wouldn't custom event handlers be a much better way of going about something like this? Is it typical in PHP to hardwire functions directly to button clicks? Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467261 Share on other sites More sharing options...
.josh Posted January 31, 2014 Share Posted January 31, 2014 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) Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467263 Share on other sites More sharing options...
Red2034 Posted January 31, 2014 Author Share Posted January 31, 2014 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... Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467264 Share on other sites More sharing options...
.josh Posted January 31, 2014 Share Posted January 31, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467266 Share on other sites More sharing options...
.josh Posted January 31, 2014 Share Posted January 31, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467268 Share on other sites More sharing options...
Red2034 Posted January 31, 2014 Author Share Posted January 31, 2014 <?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.... Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467274 Share on other sites More sharing options...
Red2034 Posted February 1, 2014 Author Share Posted February 1, 2014 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/285833-code-firing-when-i-reload-page/#findComment-1467364 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.