Jump to content

Recommended Posts

Hi all,

 

I am attempting to save the results that the user chooses from the drop down menu on my website and am running into some difficulty.

 

I have got the code to display the drop down menu, but have no idea what the code might be to post the results to the table in my Database.

 

Have had a thorough look on the internet and found some help, but most of this involved creating a new form, is there anyway to achieve this?

 

the code for my page is below.

 

 

<?php include('menu.php'); ?>

<?php

/* 

NEW.PHP

Allows user to create a new entry in the database

*/

 

// creates the new record form

// since this form is used multiple times in this file, I have made it a function that is easily reusable

function renderForm($asset_tag, $asset_type, $model, $sku, $warrenty_expiry_date, $serial_number, $location, $user_name, $mobile_number, $network, $purchase_date, $po_number, $cost, $username, $input_date, $error)

{

?>

 

 

 

<html>

 

 

<head>

 

 

 

<link rel="stylesheet" type="text/css" href="style2.css" />

 

<title> BM New Record </title>

 

 

<script type="text/javascript">

function show_alert()

{

alert("Proccessing Data..");

}

</script>

 

</head>

 

<body>

 

<span title="Words you want as the popup message"><b>Asset Tag</b></span>

 

<?php 

// if there are any errors, display them

if ($error != '')

{

echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';

}

?>

 

<form action="" method="post">

<input type="hidden" name="asset_tag" value="<?php echo $asset_tag; ?>"/>

 

<table border='1' id= 'asset_register'>

 

<td><strong>  </strong> <a href="" onMouseOver="alert('Please enter a valid Asset Tag');return true;">Asset Tag *</a>  <input type="text" name="asset_tag" value="<?php echo $asset_tag; ?>" /><br/>

<td><strong>Asset Type </strong> <input type="hidden" class="input" name="asset_type" value="Blackberry" /><br/> Blackberry

<td><strong>Model *</strong> <input type="text" class="input" name="model" value="<?php echo $model; ?>" /><br/>

<td><strong>SKU *</strong> <input type="text" class="input" name="sku" value="<?php echo $sku; ?>" /><br/>

 

<td><strong>Warrenty Exp Date *</strong> <input type="text" class="input" name="warrenty_expiry_date" value="<?php echo $warrenty_expiry_date; ?>" /><br/>

<td><strong>Serial Number *</strong> <input type="text" name="serial_number" value="<?php echo $serial_number; ?>" /><br/>

</tr>

</tr>

<td><strong>Location *</strong> <input type="hidden" class="input" name="location" value="London"<?php echo $location; ?>" /><br/> London

 

<td><strong>Practice<strong><br/>

 

<?php 

 

        // connect to the database

        include('connect-db.php');

 

        // get results from database

        $result = mysql_query("SELECT * FROM asset_register") 

                or die(mysql_error()); 

 

$query="SELECT practice,id FROM asset_register ORDER BY id";

$result = mysql_query ($query);

echo "<select practice=asset_register value=''>practice</option>";

 

// printing the list box select command

 

while($nt=mysql_fetch_array($result)){//Array or records stored in $nt

echo "<option value=$nt[id]>$nt[practice]</option>";

/* Option values are added by looping through the array */

 

 

}

 

 

 

?>

 

</tr> 

<td><strong>User Name *</strong> <input type="text" class="input" name="user_name" value="<?php echo $user_name; ?>" /><br/> 

<td><strong>Mobile Number *</strong> <input type="text" class="input" name="mobile_number" value="<?php echo $mobile_number; ?>" /><br/>

</tr> 

<br />

<td><strong>Network </strong> <input type="hidden" class="input" name="network" value="Vodaphone"<?php echo $network; ?>" /><br/> Vodaphone

<td><strong>Purchase Date *</strong> <input type="text" class="input" name="purchase_date" value="<?php echo $purchase_date; ?>" /><br/>

</tr>

</tr> 

 

<td><strong>PO Number </strong> <input type="text" class="input" name="po_number" value="<?php echo $po_number; ?>" /><br/>

<td><strong>Cost (£)* </strong> <input type="text" class="input" name="cost" value="<?php echo $cost; ?>" /><br/>

<td><strong>Input by </strong> <input type="text" class="input" name="username" value="<?php echo $username; ?>" /><br/>

 

<td><strong>Date Input </strong> <input type="hidden" class="input" name="input_date" value="<?php echo date("d/m/y") ?>" /><br/> <?php echo date("d/m/y") ?> </td>   

<td><input type="submit" name="submit" value="Submit" class="button" onclick="show_alert()" value="Show alert box" /><br/> </td>

 

 

</tr>

 

      </table> 

 

</form>

 

 

 

</body>

 

 

 

</html>

 

<br /> <br /> <br /><div align="center"> <img src="bm.jpg"></div>

       

 

 

<?php 

}

 

 

 

 

// connect to the database

include('connect-db.php');

 

// check if the form has been submitted. If it has, start to process the form and save it to the database

if (isset($_POST['submit']))

// get form data, making sure it is valid

$asset_tag = mysql_real_escape_string(htmlspecialchars($_POST['asset_tag']));

$asset_type = mysql_real_escape_string(htmlspecialchars($_POST['asset_type'])); 

$model = mysql_real_escape_string(htmlspecialchars($_POST['model']));

$sku = mysql_real_escape_string(htmlspecialchars($_POST['sku']));

$warrenty_expiry_date = mysql_real_escape_string(htmlspecialchars($_POST['warrenty_expiry_date']));

$serial_number = mysql_real_escape_string(htmlspecialchars($_POST['serial_number']));

$location = mysql_real_escape_string(htmlspecialchars($_POST['location']));

$user_name = mysql_real_escape_string(htmlspecialchars($_POST['user_name']));

$mobile_number = mysql_real_escape_string(htmlspecialchars($_POST['mobile_number']));

$network = mysql_real_escape_string(htmlspecialchars($_POST['network']));

$purchase_date = mysql_real_escape_string(htmlspecialchars($_POST['purchase_date']));

$po_number = mysql_real_escape_string(htmlspecialchars($_POST['po_number']));

$cost = mysql_real_escape_string(htmlspecialchars($_POST['cost']));

$username = mysql_real_escape_string(htmlspecialchars($_POST['username']));

$input_date = mysql_real_escape_string(htmlspecialchars($_POST['input_date']));

 

// check to make sure both fields are entered

if ($asset_tag == '' OR $cost == '0.00')

{

// generate error message

 

$error = 'ERROR: Please fill in all required fields!';

   

// if either field is blank, display the form again

renderForm($asset_tag, $asset_type, $model, $sku, $warrenty_expiry_date, $serial_number, $location, $user_name, $mobile_number, $network, $purchase_date, $po_number, $cost, $username, $input_date, $error);

}

else

{

// save the data to the database

mysql_query("INSERT asset_register SET asset_tag='$asset_tag', asset_type='Blackberry', model='$model', sku='$sku', warrenty_expiry_date='$warrenty_expiry_date', serial_number='$serial_number', location='$location', user_name='$user_name', mobile_number='$mobile_number', network='$network', purchase_date='$purchase_date', po_number='$po_number', cost='$cost', username='$username', input_date='$input_date'")

or die(mysql_error()); 

 

// once saved, redirect back to the view page

header("Location: view.php"); 

}

}

else

// if the form hasn't been submitted, display the form

{

renderForm('','','','','','','','','','','','','','','','','','','');

}

 

 

?>

 

Note: The bit in red is the code which successfully displays the drop down menu

 

Many thanks

 

Moose

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Your select option is wrong

echo "<select practice=asset_register value=''>practice</option>";

 

it should be something like this

echo '<select name="practice">practice</option>';

then you can use $_POST['practice'] to get the selected value (like any other element)

 

Hope this helps

 

Oh and PLEASE use code tags(#) as its a pain to read and some people will just skip the thread

Thanks for your quick reply.

 

Apologies for the tags - this is my first time using a forum, and first time using PHP!

 

I have implemented the changes you have suggested, with the POST function as shown below, but it still doesnt display the option that I select into the database..

 

Any ideas?

 

thanks

 

 

 

 
// connect to the database 
include('connect-db.php'); 
  
// check if the form has been submitted. If it has, start to process the form and save it to the database 
if (isset($_POST['submit'])) 
{  
// get form data, making sure it is valid 
$asset_tag = mysql_real_escape_string(htmlspecialchars($_POST['asset_tag'])); 
$asset_type = mysql_real_escape_string(htmlspecialchars($_POST['asset_type']));  
$model = mysql_real_escape_string(htmlspecialchars($_POST['model'])); 
$sku = mysql_real_escape_string(htmlspecialchars($_POST['sku']));
$warrenty_expiry_date = mysql_real_escape_string(htmlspecialchars($_POST['warrenty_expiry_date']));
$serial_number = mysql_real_escape_string(htmlspecialchars($_POST['serial_number']));
$location = mysql_real_escape_string(htmlspecialchars($_POST['location']));
$user_name = mysql_real_escape_string(htmlspecialchars($_POST['user_name']));
$mobile_number = mysql_real_escape_string(htmlspecialchars($_POST['mobile_number']));
$network = mysql_real_escape_string(htmlspecialchars($_POST['network']));
$purchase_date = mysql_real_escape_string(htmlspecialchars($_POST['purchase_date']));
$po_number = mysql_real_escape_string(htmlspecialchars($_POST['po_number']));
$cost = mysql_real_escape_string(htmlspecialchars($_POST['cost']));
$username = mysql_real_escape_string(htmlspecialchars($_POST['username']));
$input_date = mysql_real_escape_string(htmlspecialchars($_POST['input_date']));
$_POST['practice'];

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.