Jump to content

PHP Search Form - Help


blepblep

Recommended Posts

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

Cool thanks for that.

 

What do you mean make sure and use the form to load the page, not just refresh it?! I've got off the website and came back done it again and entered a number and it keeps showing the error -

 

 

There was a problem with the form, please try again and make sure you have filled in a document number

 

Link to comment
Share on other sites

Yeah I fixed that so it would run. Whenever I run it it goes to the searchByDocID page but displays nothing..

 

I honestly don't no why this isnt working, would it have anything got to do with how I have this done? As it's not giving me any errors but not displaying anything! Would you like access to my database or what?

 

while ($result = mysql_fetch_array( $data ))
{
	echo $row  ['doc_id'] . 			" <br /> " 
		. $row ['doc_title'] . 			" <br /> "
		. $row ['doc_number'] .			" <br /> "
		. $row ['doc_type'] . 			" <br /> " 
		. $row ['revision'] . 			" <br /> " 
		. $row ['cdm_link'] . 			" <br /> "
		. $row ['mars_link'] .			" <br /> "
		. $row ['checklist_link'] .		" <br /> "
		. $row ['link_internal_1_3']. 	" <br /> "
		. $row ['impacted_products'].	" <br /> "
		. $row ['scope'].				" <br /> "
		. $row ['impacted_products_2'] ." <br /> "
		. $row ['review_class'].		" <br /> "
		. $row ['full_simplified'] .	" <br /> "
		. $row ['pref_earliest'] . 		" <br /> "
		. $row ['pref_latest'] . 		" <br /> "
		. $row ['prev_reviews'] . 		" <br /> "
		. $row ['proj_name'] .			" <br /> "
		. $row ['auth_name'] . 			" <br /> "
		. $row ['req_list'] .			" <br /> "
		. $row ['optional_list'] .		" <br /> "
		. $row ['information_only'] . 	" <br /> "
		. $row ['chairperson'] .		" <br /> "
		. $row ['req_reviewers'] .		" <br /> "
		. $row ['review_duration'] .	" <br /> "
		. $row ['document_abstract'] . 	" <br /> "; 
}

Link to comment
Share on other sites

Nope, still not displaying anything :(

 

Heres a screenshot of my database if this is any help?

 

1415qq0.jpg

 

I thought by echoing ['doc_id'] etc it would display what id it was along with the ['doc_title'] and the rest of them.

 

 

This is when I browse the table -

 

xqc7b.jpg

Link to comment
Share on other sites

OK, I have this working with a sample dataset:

 

FORM PAGE :

<?php

//----------------
// Include files 
// - Connect to DB
//----------------

include "newheader.php";
include "connect_db.php";
include "lib/toolfunctions.php";

?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
</head>

<body>
		<p>
		<form action = "searchByDocID.php" method = "post">
		<!-- body onLoad focuses textbox on load
			-- Only works in Chrome at the moment -->
			<body onLoad = "documentNumber.focus()">

			<input type= "text" id= "documentNumber" name= "searchbyequipmenttype"/>
			<input type= "submit" name= "searchID" value="Search By Doc ID" /><br />

			<font size = '2'>EG: 123</font>
		</p>
	</form>		

<div id="toolFooterWrapper">
	<div id="toolFooter">

		TC Tool<br/>Version 1.00

	</div>
</div>
</body>
</html>

and

searchByDocID PAGE :

<?php
include 'connect_db.php';
include 'newheader.php';

function sanitize_data($data)
    {
        $data = array_map('trim',$data);
        $data = array_map('strip_tags',$data);
        $data = array_map('htmlspecialchars',$data);
        $data = array_map('mysql_real_escape_string',$data);
        return $data;
    }
    $post = sanitize_data($_POST);
                                       
if (isset($post['searchID']) && $post['searchbyequipmenttype'] != '')
{
$id = $post['searchbyequipmenttype'];
$sql = <<<SQL
SELECT *
FROM sampleformid
WHERE
(
doc_id = $id
)
SQL;

$data = mysql_query($sql) or die(mysql_error()."<br>-----------<br>$sql");
$num = mysql_num_rows($data);
while($row = mysql_fetch_assoc($data))
{

echo "{$row['doc_id']}<br />". 
"{$row['doc_title']}<br />".
"{$row['doc_number']}<br />".
"{$row['doc_type']}<br />". 
"{$row['revision']}<br />". 
"{$row['cdm_link']}<br />".
"{$row['mars_link']}<br />".
"{$row['checklist_link']}<br />".
"{$row['link_internal_1_3']}<br />".
"{$row['impacted_products']}<br />".
"{$row['scope']}<br />".
"{$row['impacted_products_2']}<br />".
"{$row['review_class']}<br />".
"{$row['full_or_simplified']}<br />".
"{$row['earliest_date']}<br />".
"{$row['latest_date']}<br />".
"{$row['previous_tc_reviews']}<br />".
"{$row['proj_name']}<br />".
"{$row['author_name']}<br />".
"{$row['req_list']}<br />".
"{$row['optional_list']}<br />".
"{$row['information_only']}<br />".
"{$row['chairperson']}<br />".
"{$row['reviewers_required']}<br />".
"{$row['review_duration']}<br />".
"{$row['document_abstract']}<br />";

}
}
else{
  echo "there was a problem with the form, please try again and make sure you have filled in a document number";
  }
  include 'footer.php';
?>

 

the last include thre may be off as I deleted it and put it back in (the notice was bugging me)

 

The Result I got was:

123
test
X123-V2
test
1.0
n/a
pluto
till
after
none
NARROW
still none
c
simple like me
whenever
thenever
none
test
me
no
no
probably
me
not likely
short as possible
sure is

 

 

I forgot to say - you will need to change the table name back from "FROM sampleformid" in the SQL code block, yours should read "FROM forms"

Link to comment
Share on other sites

IT WORKS!!!!! YES!

 

Thank you so much Muddy, really really appreciate it. Thanks A LOT man!!  :D :D :D :D

 

Can I ask you another thing, in my database my most recent entry is 342, when I enter 341 or below nothing appears. Is there any way I can make a message appear saying something like -

 

Sorry, no entry for that ID, please enter another.

 

Any way to do that if someone enters an ID or say 341 or below, or something like 1000?  Basically to give an error if an ID doesnt exist in the database..

 

Thanks again I really appreciate it by the way.

Link to comment
Share on other sites

$data = mysql_query($sql) or die(mysql_error()."<br>-----------<br>$sql");
$num = mysql_num_rows($data); //<<<------ This line here is counting the number of rows returned by the query

while($row = mysql_fetch_assoc($data))

 

look at the comment above.  You see how $num is being assigned with the number of rows returned from the query?  you just need to write an if statement to check that at least 1 row is fouund from the query, if it is, display the results, otherwise show your message about not finding the document.

Link to comment
Share on other sites

PHP isn't browser specific, it has no direct interface with the browser (you need javascript for that).  If it's working on one browser and not another it's most likely

 

1. your loading an old version of the page in the other browser

2. CSS is out (some browsers are stricter than others about errors in css)

3. HTML is malformed (some browsers won't render pages if the HTML code is too badly formed, others will just put up with it a bit better)

 

tick off all three of those and you *should* get it working on all browsers.

Link to comment
Share on other sites

Cool, thanks Muddy. I've one more question if you dont mind me asking. In my forms, I have certain fields that are not applicable to each forms, so in the fields that are not applicable they say N/A. But the N/A fields have to be there as they are in all of the forms.

 

The one problem I have with this is that the value N/A doesnt store in the database or display in the form when the ID is entered to search for. Have you any idea how I can get the N/A to display in the form when the ID is searched and have it store in the database too, without the user having to type it in?

 

This is one field that is N/A -

 

<tr>
    <td>Review Duration</td>
    <td><input type = "text" size = "51" name = "review_duration" value = "N/A" readonly = "readonly"/></td>		
</tr>

 

And then when searched it appears like this, the fields that are meant to say N/A are the ones that have nothing beside them like Full or Simplified etc -

 

6jdhky.jpg

 

Link to comment
Share on other sites

you don't need to store the N/A in the database, infact it's probably best that you dont do it.  all you need to do is some conditional formating on your display. Something along the lines of:

while($row = mysql_fetch_assoc($data))
//------------------------------
//this will check each result and replace any empty fields with 'N/A'
foreach($row as $k => $v){
if(trim($v) == ''){
  $v = 'N/A';
}
}
//------------------------------
{
echo "{$row['doc_id']}<br />". 

Link to comment
Share on other sites

Sorry I don't really understand to be honest. The reason I need N/A is because I'm using only oneDB, and if full or simplified isn't used in one form it is used in another if you get me?

 

If I don't store something in the database (Like N/A for example) I get an error saying Query doesn't match column count, so I have to have some value being passed into the fields that are not needed.

 

// Prints out details related to ID entered
if($num != 0)
{
while($row = mysql_fetch_assoc($data))
//------------------------------
//this will check each result and replace any empty fields with 'N/A'
foreach($row as $k => $v)
{
if(trim($v) == '')
{
  $v = 'N/A';
}
}
//------------------------------
{
echo "<b>Document ID:</b> {$row['doc_id']}<br />". 

 

But it doesnt displaying anything that was entered when I search for the document, it leaves everything blank..

 

Link to comment
Share on other sites

you can fix the Query doesn't match comlumn count by explicitly listing the fields that you want to insert into : INSERT INTO tableName (fieldname1, fieldname3, fieldname46, fieldname47) VALUES ($var1, $var2, $var3, $var4)

 

that little bit of code shouldn't affect the display at all other than to put N/A where the blanks would have been.

Link to comment
Share on other sites

Cool thanks, I fixed the query now! But now that I've gotten rid of the fields that aren't relevant to the forms (Instead of having N/A), when I search it still displays nothing if I put in the -

 

if($num != 0)
{
while($row = mysql_fetch_assoc($data))

//this will check each result and replace any empty fields with 'N/A'
foreach($row as $k => $v)
{
if(trim($v) == '')
{
  $v = 'N/A';
}
}

 

And if I take out the foreach of above, it displays the search results but still shows blank fields. Would it be better to leave the fields as N/A in the form, submit them to the database or is there a way I can do what you showed, like pass in N/A to the returned search fields so they wont be left blank?

Link to comment
Share on other sites

Let's step it up, throw away the foreach that's causing the problem, we can do the N/A thing by writing a tiny little custom function, then applying that to the output: add this code neer the top of the page:

function custOut($val){
  if (is_array($val)){
    foreach($val as $k=> $v){
      if(trim($v) == '' || empty($v) || !isset($v)){
        $v = 'N/A';
      }
    }
  }
  else{
    if(trim($val) == '' || empty($val) || !isset($val)){
      $val = 'N/A';
    }
  }
return $val;
}

then we change up the code for the display:

echo custOut($row['doc_id'])."<br />". 
custOut($row['doc_title'])."<br />".
custOut($row['doc_number'])."<br />".
custOut($row['doc_type'])."<br />". 
custOut($row['revision'])."<br />". 
custOut($row['cdm_link'])."<br />".
custOut($row['mars_link'])."<br />".
custOut($row['checklist_link'])."<br />".
custOut($row['link_internal_1_3'])."<br />".
custOut($row['impacted_products'])."<br />".
custOut($row['scope'])."<br />".
custOut($row['impacted_products_2'])."<br />".
custOut($row['review_class'])."<br />".
custOut($row['full_or_simplified'])."<br />".
custOut($row['earliest_date'])."<br />".
custOut($row['latest_date'])."<br />".
custOut($row['previous_tc_reviews'])."<br />".
custOut($row['proj_name'])."<br />".
custOut($row['author_name'])."<br />".
custOut($row['req_list'])."<br />".
custOut($row['optional_list'])."<br />".
custOut($row['information_only'])."<br />".
custOut($row['chairperson'])."<br />".
custOut($row['reviewers_required'])."<br />".
custOut($row['review_duration'])."<br />".
custOut($row['document_abstract'])."<br />"

 

That should do it.

 

Link to comment
Share on other sites

Yep, it works also, you are a genius Muddy, cannot thank you enough! Would you be able to explain this function a little please if you have the time? Not too sure how it works.

 

function custOut($val){
  if (is_array($val)){
    foreach($val as $k=> $v){
      if(trim($v) == '' || empty($v) || !isset($v)){
        $v = 'N/A';
      }
    }
  }
  else{
    if(trim($val) == '' || empty($val) || !isset($val)){
      $val = 'N/A';
    }
  }
return $val;
} 

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.