-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_opportunity_form.php
160 lines (119 loc) · 5.33 KB
/
Create_opportunity_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
// Include config file
require_once "Config.php";
// Define variables and initialize with empty values
$opportunity = $amount = $stage = $proposal="";
$opportunity_err = $amount_err = $stage_err = $proposal_err="";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// ValOidate opportunity
if(empty(trim($_POST["opportunity"]))){
$opportunity_err = "Please enter an opportunity.";
} else{
// Prepare a select statement
$sql = "SELECT Oid FROM Opportunities WHERE opportunity = :opportunity";
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":opportunity", $param_uname, PDO::PARAM_STR);
// Set parameters
$param_opportunity = trim($_POST["opportunity"]);
// Attempt to execute the prepared statement
if($stmt->execute()){
if($stmt->rowCount() == 1){
$opportunity_err = "This opportunity name is already taken.";
} else{
$opportunity = trim($_POST["opportunity"]);
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
}
// ValOidate amount
if(empty(trim($_POST["amount"]))){
$amount_err = "Please enter an amount.";
}
else{
$amount = trim($_POST["amount"]);
}
// ValOidate stage
if(empty(trim($_POST["stage"]))){
$stage_err = "Please enter stage.";
} else{
$stage = trim($_POST["stage"]);
}
// ValOidate proposal
if(empty(trim($_POST["proposal"]))){
$proposal_err = "Please enter proposal.";
} else{
$proposal = trim($_POST["proposal"]);
}
// Check input errors before inserting in database
if(empty($opportunity_err) && empty($amount_err) && empty($stage_err) && empty($proposal_err)){
// Prepare an insert statement
$sql = "INSERT INTO Opportunities (opportunity, amount, stage, proposal) VALUES (:opportunity, :amount, :stage, :proposal)";
if($stmt = $pdo->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bindParam(":opportunity", $param_opportunity, PDO::PARAM_STR);
$stmt->bindParam(":amount", $param_amount, PDO::PARAM_STR);
$stmt->bindParam(":stage", $param_stage, PDO::PARAM_STR);
$stmt->bindParam(":proposal", $param_proposal, PDO::PARAM_STR);
// Set parameters
$param_opportunity = $opportunity;
$param_amount = $amount;
$param_stage = $stage;
$param_proposal = $proposal;
// Attempt to execute the prepared statement
if($stmt->execute()){
// Redirect to login page
header("location: Create_opportunity_form.php");
} else{
echo "Something went wrong. Please try again later.";
}
// Close statement
unset($stmt);
}
}
// Close connection
unset($pdo);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset = "UTF-8">
<title>Create Opportunity form</title>
</head>
<body>
<h1>Create Opportunity</h1>
<p>Please fill this form</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($opportunity_err)) ? 'has-error' : ''; ?>">
<label>opportunity</label>
<input type="text" name="opportunity" class="form-control" value="<?php echo $opportunity; ?>">
<span class="help-block"><?php echo $opportunity_err; ?></span>
</div> </br>
<div class="form-group <?php echo (!empty($amount_err)) ? 'has-error' : ''; ?>">
<label>amount</label>
<input type="text" name="amount" class="form-control" value="<?php echo $amount; ?>">
<span class="help-block"><?php echo $amount_err; ?></span>
</div> </br>
<div class="form-group <?php echo (!empty($stage_err)) ? 'has-error' : ''; ?>">
<label>stage</label>
<input type="text" name="stage" class="form-control" value="<?php echo $stage; ?>">
<span class="help-block"><?php echo $stage_err; ?></span>
</div> </br>
<div class="form-group <?php echo (!empty($proposal_err)) ? 'has-error' : ''; ?>">
<label>Proposal</label>
<input type="text" name="proposal" class="form-control" value="<?php echo $proposal; ?>">
<span class="help-block"><?php echo $proposal_err; ?></span>
</div> </br>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit" onclick="alert('Successfully Done!')">
<input type="reset" class="btn btn-default" value="Reset">
</div>
</form>
</body>
</html>