Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I pass input from a form on one page to another page and then submit that data using PHP?

Status
Not open for further replies.

zoldos

Technical User
Nov 5, 2008
90
US
I'm sure the answer to this is easy, but I'm a php/javascript noob, hence my question. The two pieces of code, ending with "document.getElementById('searchLink').style.display = 'inline';", works visually perfectly, but now I want to figure out how to get the input entered in the first section's form passed and used for the search criteria on my search page (starting with "{include file='header.tpl'}" and ending with "{include file='footer.tpl'}"). I'd like it so the info passed automatically searches on the search page as if I had typed it there directly and hit "search". I'm using smarty templates, and have also included the back bone PHP code from my search page (starting with "$page = "search";"). I can't use Jquery as it interferes with other code, but I imagine the solution is rather straightforward.

I'd like to use sessions, but only have a vague idea how to implement this and the above, so a detailed reply would be greatly appreciated! Thanks!!


Code:
        <form id="searchForm" action="./search.php" method="post">
            <input type="text" name="new_search_text" value="">
            <input type="submit" value="Search">
            <input type="submit" value="Cancel" onclick="showSearchLink();return false;">
        </form>
        <a id="searchLink" class="button_link_main" href="./search.php" onclick="showSearchForm();return false;">Link</a>

        
        function showSearchForm() {
            // show the form by setting style="display:inline"
            document.getElementById('searchForm').style.display = 'inline';
            // hide the link by setting style="display:none"
            document.getElementById('searchLink').style.display = 'none';
        }
        
        function showSearchLink() {
            // hide the form
            document.getElementById('searchForm').style.display = 'none';
            // show the link
            document.getElementById('searchLink').style.display = 'inline';
        }
      


    {include file='header.tpl'}
       
    <img src='./images/icons/search48.gif' border='0' class='icon_big'>
    <div class='page_header'>Search Avulsion Net</div>
    <div>{lang_print id=924}</div>
    <br />
    <br />
    
    <form action='search.php' name='search_form' method='post'>
    <table cellpadding='0' cellspacing='0' align='center'>
    <tr>
    <td class='search'>
      <table cellpadding='0' cellspacing='0' align='center'>
      <tr>
      <td>{lang_print id=925}</td>
      <td>&nbsp;<input style='background-color:#CCCCCC' type='text' size='30' class='text' name='search_text' id='search_text' value='{$search_text}' maxlength='100'></td>
      <td>
        &nbsp;<input type='submit' class='button' value='{lang_print id=646}'>
        <input type='hidden' name='task' value='dosearch'>
        <input type='hidden' name='t' value='0'>
      </td>
      </tr>
      <tr>
      <td>&nbsp;</td>
      <td colspan='2'>&nbsp;<b>-=<a href='search_advanced.php'>Browse Members</a>=-</b></td>
      </tr>
      </table>
    </div>
    </form>
    </td>
    </tr>
    </table>
    
    <br>
    
    {if $search_text != ""}
    
      {if $is_results == 0}
    
        <table cellpadding='0' cellspacing='0' align='center'>
        <tr>
        <td class='result'>
          <img src='./images/icons/bulb16.gif' class='icon'>
          {lang_sprintf id=927 1=$search_text}
        </td>
        </tr>
        </table>
    
      {else}
    
    
        {* SHOW DIFFERENT RESULT TOTALS *}
        <table class='tabs' cellpadding='0' cellspacing='0'>
        <tr>
        <td class='tab0'>&nbsp;</td>
          {section name=search_loop loop=$search_objects}
            <td class='tab{if $t == $search_objects[search_loop].search_type}1{else}2{/if}' NOWRAP>{if $search_objects[search_loop].search_total == 0}{lang_sprintf id=$search_objects[search_loop].search_lang 1=$search_objects[search_loop].search_total}{else}<a href='search.php?task=dosearch&search_text={$url_search}&t={$search_objects[search_loop].search_type}'>{lang_sprintf id=$search_objects[search_loop].search_lang 1=$search_objects[search_loop].search_total}</a>{/if}</td>
            <td class='tab'>&nbsp;</td>
          {/section}
          <td class='tab3'>&nbsp;</td>
        </tr>
        </table>
    
        <div class='search_results'>
    
          {* SHOW PAGES *}
          {if $p != 1}<a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p-1' p=$p}'>&#171; {lang_print id=182}</a> &nbsp;|&nbsp;&nbsp;{/if}
          {if $p_start == $p_end}
            <b>{lang_sprintf id=184 1=$p_start 2=$total_results}</b> ({lang_sprintf id=928 1=$search_time}) 
          {else}
            <b>{lang_sprintf id=185 1=$p_start 2=$p_end 3=$total_results}</b> ({lang_sprintf id=928 1=$search_time}) 
          {/if}
          {if $p != $maxpage}&nbsp;&nbsp;|&nbsp; <a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p+1' p=$p}'>{lang_print id=183} &#187;</a>{/if}
    
          <br><br>
    
          {* SHOW RESULTS *}
          {section name=result_loop loop=$results}
    	
    	<div class='search_result{cycle name="class_name" values="1,2,2,1"}' style='width: 400px; float: left; border: 1px solid #CCCCCC; margin: 5px;'>
    	<table cellpadding='0' cellspacing='0'>
            <tr>
            <td valign='top' style='padding-right: 4px;'>
    	  <a href="{$results[result_loop].result_url}" class="title"><img src='{$results[result_loop].result_icon}' class='photo' width='60' height='60' border='0'></a>
    	</td>
    	<td valign='top'>
              <div class='search_result_text'>
    	    {capture assign='result_title'}{lang_sprintf id=$results[result_loop].result_name 1=$results[result_loop].result_name_1}{/capture}
                <a href="{$results[result_loop].result_url}" class="title">{$result_title|truncate:40:"...":true}</a>
                <div class='search_result_text2'>{lang_sprintf id=$results[result_loop].result_desc 1=$results[result_loop].result_desc_1 2=$results[result_loop].result_desc_2 3=$results[result_loop].result_desc_3}</div>
    	    {if $results[result_loop].result_online == 1}<div style='margin-top: 5px;'><img src='./images/icons/online16.gif' border='0' class='icon'>{lang_print id=929}</div>{/if}
              </div>
    	</td>
    	</tr>
    	</table>
    	</div>
            {cycle name="clear_cycle" values=",<div style='clear: both; height: 0px;'></div>"}
          {/section}
    
          <div style='clear:both;'></div><br />
    
          {* SHOW PAGES *}
          {if $p != 1}<a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p-1' p=$p}'>&#171; {lang_print id=182}</a> &nbsp;|&nbsp;&nbsp;{/if}
          {if $p_start == $p_end}
            <b>{lang_sprintf id=184 1=$p_start 2=$total_results}</b> ({lang_sprintf id=928 1=$search_time}) 
          {else}
            <b>{lang_sprintf id=185 1=$p_start 2=$p_end 3=$total_results}</b> ({lang_sprintf id=928 1=$search_time}) 
          {/if}
          {if $p != $maxpage}&nbsp;&nbsp;|&nbsp; <a href='search.php?task=dosearch&search_text={$url_search}&t={$t}&p={math equation='p+1' p=$p}'>{lang_print id=183} &#187;</a>{/if}
    
    
        </div>
      {/if}
    {/if}
    
    
    {* JAVASCRIPT TO AUTOFOCUS ON SEARCH FIELD *}
    {literal}
    <script type="text/javascript">
    <!-- 
      window.addEvent('load', function(){ $('search_text').focus(); });
    //-->
    </script>
    {/literal}
    
    
    {include file='footer.tpl'}

    

    <?php
    
    $page = "search";
    include "header.php";
    
    // DISPLAY ERROR PAGE IF USER IS NOT LOGGED IN AND ADMIN SETTING REQUIRES REGISTRATION
    if($user->user_exists == 0 && $setting['setting_permission_search'] == 0)
    {
      $page = "error";
      $smarty->assign('error_header', 639);
      $smarty->assign('error_message', 656);
      $smarty->assign('error_submit', 641);
      include "footer.php";
    }
    
    if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
    if(isset($_POST['p'])) { $p = (int) $_POST['p']; } elseif(isset($_GET['p'])) { $p = (int) $_GET['p']; } else { $p = 1; }
    if(isset($_POST['search_text'])) { $search_text = $_POST['search_text']; } elseif(isset($_GET['search_text'])) { $search_text = $_GET['search_text']; } else { $search_text = ""; }
    if(isset($_POST['t'])) { $t = $_POST['t']; } elseif(isset($_GET['t'])) { $t = $_GET['t']; } else { $t = 0; }
    
    // SET VARS
    $results_per_page = 20;
    $results = Array();
    $total_results = 0;
    $is_results = 0;
    $object_count = 0;
    $search_objects = Array();
    $is_next_page = 0;
    if($p < 1) { $p = 1; }
    
    
    
    // DO SEARCH
    if($task == "dosearch" && $search_text != "")
    {
      // START SEARCH TIMER
      $start_timer = getmicrotime();
    
      // SEARCH PROFILES
      search_profile();
    
      // CALL SEARCH HOOK
      ($hook = SE_Hook::exists('se_search_do')) ? SE_Hook::call($hook, array()) : NULL;
    
      // GET GRAND TOTAL RESULTS
      for($r=0;$r<count($search_objects);$r++)
      {
        if($search_objects[$r][search_total] != 0)
        { 
          if($total_results == 0) { header("Location: search.php?task=dosearch&search_text=".urlencode($search_text)."&t=".$search_objects[$r]['search_type']); exit(); }
          $is_results = 1; 
        }
      }
    
      // END TIMER
      $end_timer = getmicrotime();
      $search_time = round($end_timer - $start_timer, 3); 
    
      // CHECK TO SEE IF THERE IS A "NEXT PAGE"
      if(count($results) > $results_per_page)
      { 
        $is_next_page = 1;
        while(count($results) > $results_per_page)
        {
          array_pop($results);
        }
      }
    
      // IF TOTAL RESULTS IS MORE THAN 200, CHANGE TO 200+
      if($total_results > 200)
      { 
        if($is_next_page == 1) { $maxpage = $p+1; } else { $maxpage = $p; }
        $total_results = "200+";
      }
      else
      {
        if(($total_results % $results_per_page) != 0) { $maxpage = ($total_results) / $results_per_page + 1; } else { $maxpage = ($total_results) / $results_per_page; }
        $maxpage = (int) $maxpage; 
      }
    
      // IF RESULTS IS EMPTY AND PAGE ISN'T 1, DISPLAY NOTHING
      if(count($results) == 0 && $p != 1) { $search_text = ""; }
    }
    
    
    
    // SET THE GLOBAL PAGE TITLE
    $global_page_title[0] = 646;
    $global_page_description[0] = 924;
    
    
    // ASSIGN SMARTY VARIABLES AND INCLUDE FOOTER
    $smarty->assign('search_text', $search_text);
    $smarty->assign('url_search', urlencode($search_text));
    $smarty->assign('is_results', $is_results);
    $smarty->assign('results', $results);
    $smarty->assign('total_results', $total_results);
    $smarty->assign('search_objects', $search_objects);
    $smarty->assign('search_time', $search_time);
    $smarty->assign('maxpage', $maxpage);
    $smarty->assign('t', $t);
    $smarty->assign('p', $p);
    $smarty->assign('p_start', (($p-1)*$results_per_page)+1);
    $smarty->assign('p_end', (($p-1)*$results_per_page)+count($results));
    include "footer.php";
    ?>
 
quick primer on sessions

Code:
if(session_id() == '') session_start();

to store a value between page refreshes
Code:
$_SESSION['some arbitrary key name'] = 'some arbitrary data';
/* you can store anything that you can ordinarily assign to a variable */
$_SESSION['myArray'] = array('apples','oranges');
/*EXCEPT resources */
$result = mysql_query('some sql');
$_SESSION['resource'] = $result;  // this will not work

/* and if you store an object, php must be able to get the class definition first*/
$_SESSION['myClass'] = new myObject;

to use the stored values on the next page just refer to the session key
Code:
var_dump($_SESSION['some arbitrary key name']);

to delete a session key
Code:
unset($_SESSION['some arbitrary key name']);

remember always to close your session explicitly when you are using ajax with relatively frequent data exchanges (anything under 20 seconds) or when testing on a local network or local machine.

Code:
session_write_close();
do this as early as you can in your code. this forces php to write to the session store and to save the file, hopefully lessening the chances of a session race condition (where the next script tries to access the session store before the last has had a chance to save it).
 
I've given up on this project. It was supposed to be a simple cosmetic mod that has grown too complex for me to figure out without someone simply using the code I provided and completely writing the correct, working answer. Thanks for the replies tho! :)
 
Okay, I'm trying this project again. Can anyone offer a more detailed answer, including code incorporating the source I've posted? I know almost nothing about sessions, PHP, or Javascript....

Thanks!
 
We don't write code for you at Tek-Tip, we provide pointers so you can LEARN how to code.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Cool, and I certainly appreciate the help! I'm actually really close to solving it now with the help of a friend on another site. Thanks!
 
I don't think that this is a php question. nor a javascript question. This looks like a basic html question.

although, from the look of the code, it will only work if register globals is turned on in php, which is now considered a security risk (or rather it has been for years). This may be an artefact of smarty however - which I have long hated.

if you change the first script as shown, the second should need no alteration.

Code:
	<form id="searchForm" action="search.php" method="post">
            <input type="text" name="search_text" value="">
            <input type="submit" value="Search">
            <input type='hidden' name='task' value='dosearch'>
            <input type='hidden' name='t' value='0'>
            <input type="reset" value="Cancel" onclick="showSearchLink();return false;">
        </form>
        <a id="searchLink" class="button_link_main" href="./search.php" onclick="showSearchForm();return false;">Link</a>

        
        function showSearchForm() {
            // show the form by setting style="display:inline"
            document.getElementById('searchForm').style.display = 'inline';
            // hide the link by setting style="display:none"
            document.getElementById('searchLink').style.display = 'none';
        }
        
        function showSearchLink() {
            // hide the form
            document.getElementById('searchForm').style.display = 'none';
            // show the link
            document.getElementById('searchLink').style.display = 'inline';
        }
 
Well look at that, it works. lol I've been going bonkers the last three days working on this too. :) Thank you VERY much!!! :)
 
My last question should be as simple (hopefully). This is my live code and it works great (note: I changed the form ID to match my modded JS, thanks again!)

Code:
<form id="searchForm_newsearch" action="search.php" method="post">
            <input type="text" name="search_text" value="">
            <input type="submit" value="Search">
            <input type='hidden' name='task' value='dosearch'>
            <input type='hidden' name='t' value='0'>
            <input type="reset" value="Cancel" onclick="showSearchLink();return false;">
        </form><a id="searchLink" class="button_link_main" href="./search.php" onclick="showSearchForm();return false;"><img src="/images/search.png" onmouseover="this.src='/images/searchsite_hover.png'" onmouseout="this.src='/images/search.png'" /></a>

But how do I get the cursor to auto-focus on the search input box when someone clicks the search button?
 
ah... now that IS a javascript question.

personally I'd load jQuery and force it into compatibility mode (so that it does not interfere as you previously mentioned). then use that to focus. But that's laziness.

normally I'd assign an ID to the searchbox and then reference that directly, but since you only have a name attached the below should work ok. not tested it though, and it's a long time since I coded in non-jQuery javascript (!!)

Code:
function showSearchForm() {
  // show the form by setting style="display:inline"
  var frm = document.getElementById('searchForm');
  frm.style.display = 'inline';
  frm.search_text.focus();  

  // hide the link by setting style="display:none"
  document.getElementById('searchLink').style.display = 'none';
}
 
You rule! It works perfectly. hehehe Thank you very much! I knew I shouldn't have dropped out of that advanced web design class in 2001. lol
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top