Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute jQuery code depending upon the value received through querystring?

Tags:

jquery

php

smarty

Let me explain the question in detail. I'm using PHP and smarty. I'm getting a value op=back from query-string. But this is not going to happen everytime the PHP file runs, so when op=back is the value I've to fir a click event on a specific link. My PHP file code snippet is given below :

<?php  
  include_once("includes/teacher-application-header.php");

  prepare_request();
  $request = empty( $_GET ) ? $_POST : $_GET ;
  $op = $request['op'];

  $objTeachClassSub = new TeacherClassesSubjects();

  global $teacher_profile_from_session;

  $teacher_id = $teacher_profile_from_session['TEACHER_ID'];

  $teacher_classes_subjects = $objTeachClassSub->GetClassSubjectMappingsbyTeacherId($teacher_id);

$smarty->assign('teacher_classes_subjects', $teacher_classes_subjects); 

  $smarty->assign("op",$op);        
  $smarty->display("teacher-details.tpl");      
?>

Now the code snippet from smarty file is as below:

{literal}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
</script>
{/literal}
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
    <tr>
        <td align="left" valign="top">
          <h3>Teacher Details</h3>
        </td>
    </tr>
</table>
<table width="99%" border="0" cellpadding="0" cellspacing="0" class="manage_box" >
    <tr>
      <td>
            <table cellpadding="0" cellspacing="0" border="1" width="100%">
        <tr>
            <td width="25%">
            {if $teacher_classes_subjects}
               {foreach from=$teacher_classes_subjects item="classes_subjects"}
                 <b><a href="#" id="{$classes_subjects.class_id}">{$classes_subjects.class_name}</a></b><br />
                 {if $classes_subjects.class_subjects}
                 <div id="class_subjects_{$classes_subjects.class_id}">
                   {foreach from=$classes_subjects.class_subjects item="class_subjects"}
                        <i><a id ="back" href="chapter_details.php?class_id={$classes_subjects.class_id}&cs_map_id={$class_subjects.cs_map_id}">{$class_subjects.subject_name}</a></i><br />
                   {/foreach}
                 </div>
                 <br />
                 {/if}
               {/foreach}
            {/if}
          </td>  
          <td width="75%">
          {if $chapter_details}
            <ul>
            {foreach from=$chapter_details item=chapter}
            <li><a href="chapter_details.php?op=get_chapter_theory&chapter_id={$chapter.chapter_id}&class_id={$class_id}&cs_map_id={$cs_map_id}&chapter_title={$chapter.chapter_title}">{$chapter.chapter_title}</a></li>
            {/foreach}
            </ul>
          {/if}

          {include file=$file_to_show}
          </td>  aly what is
        </tr>
      </table>
        </td>
    <td align="left" id="subject_container" valign="top">
    </td>
    <td align="left" id="chapter_container" valign="top">
    </td>
  </tr>
</table>

Now what I want to achieve is to write a jQuery code in the above smart template file which will execute if the op value is back(i.e.{op==back}). Actually what is expected is when the op=back click event should get fire on the following link:

<a id ="back" href="chapter_details.php?class_id={$classes_subjects.class_id}&cs_map_id={$class_subjects.cs_map_id}">{$class_subjects.subject_name}</a>

Can anyone explain me how to achieve this functionality? Thanks in Advance.

like image 520
PHPLover Avatar asked Nov 03 '22 21:11

PHPLover


2 Answers

If you want to go to an specific site when op==back you should just redirect it in you php code

 if(isset($_GET['op']) && $_GET['op'] == 'back'){
      header('location: yourpath/youurl.php');
 }

Remember to do this before echoing anything.

If you want to trigger a click when this option is set just print an script conditionally with a document ready and a $('#back').trigger('click') if you are using jQuery:

 //$go = (isset($_GET['op']) && $_GET['op'] == 'back');
 {if $go}
 <script>
   $(document).ready(function(){
        $('#back').trigger('click');
   });
 </script>
 {/if}

EDIT

Clicking on that link will make the browser got to another page, unless you have used something like e.preventDefault(). If you haven't I would recommend using the first solution i proposed. If you need the request to get to the client, an then redirect, for any reason, or you do something else I would use the second solution.

Hope it helps

like image 126
Nickydonna Avatar answered Nov 09 '22 11:11

Nickydonna


I guess your best option is to set it to a hidden html input in your template, and in your java script in the document ready checking the value of that html input, then changing the href attribute of your link

like image 28
abma Avatar answered Nov 09 '22 11:11

abma