• 0

Some help with PHP please


Question

Hello,

I would appreciate some pointers on PHP. Im very much a beginner and am not sure how to get my head around what i want to achieve.

Basically, i have uploaded a stock list to a MySQL database. I have then created a simple search form using PHP to display the results.

What i would like, is for each result to have a check box next to it, which the user could then tick and then click to request a quotation for those parts that have been selected. Have a look here at what i currently have, and here at what i am trying to achieve.

Here is all i have so far:

<?php
// Check if form submitted
if (isset($_POST['Search']) || isset($_POST['Alternatives'])){

	// Get form input
	$PartSearch = $_POST['PartNumber'];

	// Strip everything except letter, digit, underscore, dash or space
	$PartSearch = preg_replace("/[^\w| |-]/i", "", $PartSearch);

	echo "Results for \"". $PartSearch ."\":<br /><br />";

	// Connect to database
	mysql_connect("localhost", "Username", "Password") or die(mysql_error());
	mysql_select_db("Database") or die(mysql_error());

	// Search the database
	$query = mysql_query("SELECT * FROM `Stock Search` WHERE `Part Number` LIKE '%$PartSearch%'") or die(mysql_error());

	// Display the results
	$NumberResults = mysql_num_rows($query);

	if ($NumberResults == 0) {
		echo "No exact results were found for: \"".$PartSearch."\"<br />";
		echo "<a href=\"/search.php\">Click here</a> to go back <br />";
	} else {
		echo "$NumberResults matching parts found <br />";
		echo "<a href=\"/search.php\">Click here</a> to go back <br />";
		echo "<table border=\"1\" align=\"center\">";
		echo "<tr><th>Part Number</th>";
		echo "<th>Manufacturer</th>";
		echo "<th>Quantity</th>";
		echo "<th>RFQ</th></tr>";
		while ($rows = mysql_fetch_array($query)) {
			echo "<tr><td>";
			echo $rows['Part Number'];
			echo "</td><td>";
			echo $rows['Manufacturer'];
			echo "</td><td>";
			echo $rows['Quantity'];
			echo "</td><td>";
			echo "<input type=\"checkbox\" value=\"1\" name=\"RFQ\"></td></tr>";
		}
		echo "</table>";
	}
} else {
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	Part Number:  <input name="PartNumber" type="text" /><br />
	<input name="Search" type="submit" value="Search" />
</form>

<?php
	}
?> 

Thanks for any help

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Other people here would know exactly how to do this, but I think you can do an Array with the checkbox. So the name of the checkbox would be something like RFQ[] and the value would be the part number. When you get this in PHP you can then parse the array to see what they selected.

Link to comment
Share on other sites

  • 0

yea all you need to do is change this

 echo "<input type=\"checkbox\" value=\"1\" name=\"RFQ\"></td></tr>";

to this

 echo "<input type=\"checkbox\" value=\"".$rows['Part Number']."\" name=\"RFQ[]\"></td></tr>";

edit: and might i suggest using single quotes ( ' ) instead of double quotes ( " ) so you don't have to escape every attribute in your html. it will make it look a lot cleaner.

Link to comment
Share on other sites

  • 0

Thanks for the replies, ill have a play now.

@mollick2 - what do you mean about making the HTML cleaner? Despite my Googling, ive never really understood the benefits of single quotes over double quotes (or vice versa), could you post an example using the code adove?

Thanks

Link to comment
Share on other sites

  • 0

Thanks for the replies, ill have a play now.

@mollick2 - what do you mean about making the HTML cleaner? Despite my Googling, ive never really understood the benefits of single quotes over double quotes (or vice versa), could you post an example using the code adove?

Thanks

Single Quotes and Double quotes are both valid in XHTML 1.0 (which is what I code in) and so it shouldn't matter which you use. My preference is double quotes. Mollick2 is indicating that he likes to use single quotes because it makes his PHP look cleaner because there is less escaping (i.e.: \") that needs to be done.

Link to comment
Share on other sites

  • 0

Ok, well ive had a play using a forum post i found, and came up with this:

 // Check if form submitted if (isset($_POST['BuildRFQ'])) { 	$RFQ = $_POST["RFQ"]; 	$how_many = count($RFQ); 	echo 'RFQs chosen: '.$how_many.'<br><br>'; 	if ($how_many>0) { 	echo 'You chose the following:<br>'; 	} 	for ($i=0; $i<$how_many; $i++) { 	echo ($i+1) . '- ' . $RFQ[$i] . '<br>'; 	} 	echo "<br><br>"; } if (isset($_POST['Search'])){ 	// Get form input 	$PartSearch = $_POST['PartNumber']; 	// Strip everything except letter, digit, underscore, dash or space 	$PartSearch = preg_replace("/[^\w| |-]/i", "", $PartSearch); 	echo 'Results for "'.$PartSearch.'" :<br /><br />'; 	// Connect to database 	mysql_connect("localhost", "Username", "Password") or die(mysql_error()); 	mysql_select_db("Database") or die(mysql_error()); 	// Search the database 	$query = mysql_query("SELECT * FROM `Stock Search` WHERE `Part Number` LIKE '%$PartSearch%'") or die(mysql_error()); 	// Display the results 	$NumberResults = mysql_num_rows($query); 	if ($NumberResults == 0) { 		echo 'No results were found for: "'.$PartSearch.'"<br />'; 		echo "<a href=\"/search.php\">Click here</a> to go back <br />"; 	} else 	{ 		echo "$NumberResults matching parts found <br />"; 		echo "<a href=\"/search.php\">Click here</a> to go back <br />"; 		echo "<table border=\"1\" align=\"center\">"; 		echo "<tr><th>Part Number</th>"; 		echo "<th>Manufacturer</th>"; 		echo "<th>Quantity</th>"; 		echo "<th>RFQ</th></tr>"; 		while ($rows = mysql_fetch_array($query))	{ 			echo "<tr><td>"; 			echo $rows['Part Number']; 			echo "</td><td>"; 			echo $rows['Manufacturer']; 			echo "</td><td>"; 			echo $rows['Quantity']; 			echo "</td><td>"; 			echo "<input type=\"checkbox\" value=\"".$rows['Part Number']."\" name=\"RFQ[]\"></td></tr>"; 		} 		echo "</table>"; 		echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">'; 		echo "<input type=\"submit\" name =\"BuildRFQ\" value=\"Request Quotation\">"; 		echo '</form>'; 	} } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 	Part Number:  	<input name="PartNumber" type="text" /><br /> 						<input name="Search" type="submit" value="Search" /> </form> <?php 	} ?> 

$how_many always evaluates to zero and i dont see why? I have been using the fruit example here

Any ideas?

EDIT: Got it, i needed to move the form opening line to encompass the whole table and all the checkbox options.

Link to comment
Share on other sites

  • 0

What mollick meant is changing your secondary quotes to a different type of quote to avoid escaping.

ie. from:

echo "<table border=\"1\" align=\"center\">";

to:

echo "<table border='1' align='center'>";

Not entirely necessary, but the end result is easier to read.

Link to comment
Share on other sites

  • 0

Hello, I seem you should use form builder as a general function of php form, this function gives an opportunity to use to establish such possibilities for this form. This method is rather good for beginners.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.