antbates1991 Posted February 20, 2014 Share Posted February 20, 2014 HI I'm creating a booking system in PHP that allows estate agents to book viewings for houses. I am having a slight issue. The concept is simple, a user searches for the property using postal code, the database then returns the property results. The user then ticks a checkbox next to the property they want to add the viewing for. This is where the issue is occurring. Each property has an ID. The ID is assigned to the name="" element in the HTML. I'm now not sure where to go from here. What I want is this, once the user selects the property, the property ID for the selected row needs to be stored so that I can use it later on in the booking. Here is the part of the Code I have. $newrecord = "Here are the results"; if (isset($_POST['submitted'])) { $PostCode = $_POST['PostCode']; $error; $checkpostcode = "SELECT * from PropertyTable WHERE PostCode = '$PostCode'"; $resultpostcode = mysql_query($checkpostcode); $count = mysql_num_rows($resultpostcode); if ($count == 0) { $_POST["PostCode"] = $PostCode; echo 'Sorry no properties were found with this Post Code. Please check it is entered correctly, then try again.'; } else { //show options $selectproperty ="SELECT * from PropertyTable WHERE PostCode = '$PostCode'"; $propertyresults = mysql_query($selectproperty); echo"<div class='page-restrict-output'>"; echo "<form method='post' action='add-viewing'>"; echo "<input id='addviewingbutton' type='Submit' name='addviewingbtn' value='Select Property' /><br><br /></td></tr>"; echo"<table id='selectviewingtable' border='1'>"; echo "<tr><th>Property Name</th><th>Post Code</th><th>Property ID</th><th>Add Viewing</th></tr>"; while($row = mysql_fetch_array($propertyresults)) { echo "<tr><td>"; echo $row['PropertyName']; echo "</td><td>"; echo $row['PostCode']; echo "</td><td>"; echo $row['ProductID']; echo "</td><td>"; echo $row['SelectViewing']; echo "<input id='selectviewingbutton' type='checkbox' name='$row[ProductID]' value='True' /></td></tr>"; echo" </form>"; echo "</div>"; } } } if (isset($_POST['addviewingbtn'])) { foreach ($_POST as $key => $value){ if ($value="True"){ $ProductId = $key; } } $choseproperty = "SELECT ProductID from PropertyTable WHERE '$row[ProductID]' = True"; $chosenpropertyresults = mysql_query($choseproperty); while($row = mysql_fetch_array($chosenpropertyresults)) { } print_r($_POST); echo "$chosenproperty"; } else { } Using print_r I get Array ( [addviewingbtn] => Select Property [4] => True ). The [4] is the property ID, this changes on which item you select. I'm just a bit lost here. I'm still getting to grips with PHP and any help would be much appreciated. Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 20, 2014 Share Posted February 20, 2014 assuming you want to allow the user to select several results, I would give all the checkboxes the same name (i.e an array) and put the ProductID in the value field. something like this: echo '<input id="selectviewingbutton" type="checkbox" name="selectedProductIDS[]" value=" . $row[ProductID] . " /></td></tr>'; when this get's POSTED to your script, you'll have an array of selected ids inside $_POST['selectedProductIDS'], you can loop through it and get the values with something like: foreach($_POST['selectedProductIDS'] as $pid){ echo $pid .'<br>'; } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 20, 2014 Share Posted February 20, 2014 As WebStyles Says Quote Link to comment Share on other sites More sharing options...
antbates1991 Posted February 20, 2014 Author Share Posted February 20, 2014 Ah thank you very much thats what I needed Quote Link to comment Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 Ok I'm having trouble with the next stage. It's probably something stupid but I can't seem to figure out why. After the user selects a property, the property variable is stored for use later. This works. PHP then echoes out another form when the Select Property button is pressed. The form that is echoed out allows a user to enter an Employee ID (to assign an employee to the viewing). This form is echoed out and it has a button called Add Employee. Once this button is pressed the Employee ID the user has entered is checked against the database to ensure its a legitimate entry and if it is the Employee ID is stored in a variable for use later. The system then echoes out saying you have selected employee number [variable that has been stored]. The problem is here. Instead of doing any of this the form just redirects to the start once the add employee button is pressed. No error messages. No success messages. It's probably something small and stupid but I can't seem to find it looking at each line individually. Here is the code. $newrecord = "Your viewing was added."; if (isset($_POST['submitpostcode'])) { $SearchPostCode = $_POST['SearchPostCode']; $error; $checkpostcode = "SELECT * from PropertyTable WHERE PostCode = '$SearchPostCode'"; $resultpostcode = mysql_query($checkpostcode); $count = mysql_num_rows($resultpostcode); if ($count == 0) { $_POST["SearchPostCode"] = $SearchPostCode; echo 'Sorry no properties were found with this Post Code. Please check it is entered correctly, then try again.'; } else { //show options $selectproperty ="SELECT * from PropertyTable WHERE PostCode = '$SearchPostCode'"; $propertyresults = mysql_query($selectproperty); echo"<div class='page-restrict-output'>"; echo "<form method='post' action='add-viewing'>"; echo "<input id='selectpropertybtn' type='Submit' name='selectpropertybtn' value='Select Property' /><br><br /></td></tr>"; echo "Here are the results, Select only ONE property or the operation will FAIL. <br>"; echo "<br>"; echo"<table id='selectviewingtable' border='1'>"; echo "<tr><th>Property Name</th><th>Post Code</th><th>Property ID</th><th>Add Viewing</th></tr>"; while($row = mysql_fetch_array($propertyresults)) { echo "<tr><td>"; echo $row['PropertyName']; echo "</td><td>"; echo $row['PostCode']; echo "</td><td>"; echo $row['ProductID']; echo "</td><td>"; echo $row['SelectViewing']; echo "<input id='selectviewingbutton' type='checkbox' name='selectedProductIDS[]' value='$row[ProductID]' /></td></tr>"; echo" </form>"; mysql_real_escape_string($PostCode); mysql_real_escape_string($newrecord); } } } if (isset($_POST['selectpropertybtn'])) { $_POST['selectedProductIDS']; { echo "<form action='add-viewing' autocomplete='on' method='post'>"; foreach($_POST['selectedProductIDS'] as $PropertyIDforviewing){ echo "<br>"; echo "You have Selected Property Number $PropertyIDforviewing.<br>"; echo "<br>"; } echo "Please Enter Your Employee ID Number."; echo "<br>"; echo "<br>"; echo "<label for='addEmployeeID'>Employee ID*:</label><br>"; echo "<input id='addEmployeeID' type='number' name='$addEmployeeID' required /><br>"; echo "<br>"; echo "<input id='addemployeebtn' type='submit' name='addemployeebtn' value='Add Employee' /><br>"; echo "</form>"; } if (isset($_POST['addemployeebtn'])) { $addEmployeeID = $_POST['addEmployeeID']; { echo "You have Selected Employee Number $addEmployeeID.<br>"; $selectemployee = "SELECT * from EmployeeTable WHERE EmployeeID = '$addEmployeeID'"; $resultselectemployee = mysql_query($selectemployeee); $count = mysql_num_rows($resultselectemployee); } if ($count == 0) { $_POST["addEmployeeID"] = $addEmployeeID; echo 'Sorry no employees were found with this ID Number. Please check it is entered correctly, then try again.'; } else { $selectemployee = "SELECT * from EmployeeTable WHERE EmployeeID = '$addEmployeeID'"; $resultselectemployee = mysql_query($selectemployeee); $count = mysql_num_rows($resultselectemployee); $row = mysql_fetch_assoc($resultselectemployee); } } } Quote Link to comment Share on other sites More sharing options...
Solution WebStyles Posted February 25, 2014 Solution Share Posted February 25, 2014 change this echo "<input id='addEmployeeID' type='number' name='$addEmployeeID' required /><br>"; to this echo "<input id='addEmployeeID' type='number' name='addEmployeeID' required /><br>"; (changed name). I didn't read through all your code, but saw this and saw you were trying to grab $_POST['addEmployeeID'] further down, so I'm guessing that's the problem.) hope this helps Quote Link to comment Share on other sites More sharing options...
WebStyles Posted February 25, 2014 Share Posted February 25, 2014 P.S. Why are you doing this $_POST["SearchPostCode"] = $SearchPostCode; // Why are you assigning a value to $_POST like this? // further down... if (isset($_POST['selectpropertybtn'])) { $_POST['selectedProductIDS']; // what's this supposed to do? { Quote Link to comment Share on other sites More sharing options...
antbates1991 Posted February 25, 2014 Author Share Posted February 25, 2014 (edited) Thanks that solved the issue. As for $_POST['selectedProductIDS'] it was incomplete it now reads foreach($_POST['selectedProductIDS'] as $PropertyIDforviewing){ Edited February 25, 2014 by antbates1991 Quote Link to comment 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.