-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd.php
More file actions
169 lines (156 loc) · 4.83 KB
/
add.php
File metadata and controls
169 lines (156 loc) · 4.83 KB
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
161
162
163
164
165
166
167
168
169
<?php
require_once "pdo.php";
require_once 'util.php';
session_start();
//Demand a user to be logged in
if (! isset($_SESSION['name']) ) { //this name is the name in the user table
die('ACCESS DENIED');
}
if (isset($_POST['cancel'])) {
header("Location: index.php");
return;
}
//Adding data
if ( isset($_POST['first_name']) && isset($_POST['last_name'])
&& isset($_POST['email']) && isset($_POST['headline'])
&& isset($_POST['summary']) && isset($_POST['profile_id']) ) {
// Data validation
$msg = validateProfile();
if (is_string($msg)){
$_SESSION['error'] = $msg;
header("Location: add.php");
return;
}
//Positions validation if present
$msg = validatePos();
if (is_string($msg)) {
$_SESSION['error'] = $msg;
header("Location: add.php");
return;
}
//Validate education data
$msg = validateEdu();
if (is_string($msg)) {
$_SESSION['error'] = $msg;
header("Location: add.php");
return;
}
//Everything correct, then insert Profile data
$sql = "INSERT INTO Profile (user_id, first_name, email, headline, last_name, summary)
VALUES (:user_id, :first_name, :email, :headline, :last_name, :summary)";
//echo("<pre>\n".$sql."\n</pre>\n");
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':first_name' => $_POST['first_name'],
':email' => $_POST['email'],
':headline' => $_POST['headline'],
':last_name' => $_POST['last_name'],
':user_id' => $_SESSION['user_id'],
':summary' => $_POST['summary'])
);
$profile_id = $pdo->lastInsertId();
//Insert Positions
insertPositions($pdo, $profile_id);
//Insert Educations
insertEducations($pdo, $profile_id);
$_SESSION['success'] = "Profile added";
header('Location: index.php');
return;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Porfirio Hernandez</title>
<?php require_once "head.php" ?>
</head>
<body>
<div class="container">
<?php
if ( isset($_SESSION['name']) ) {
echo "<h1>Adding Profile for ";
echo htmlentities($_SESSION['name']);
echo "</h1>\n";
}
flashMessages();
?>
<form method="post">
<p>First Name:
<input type="text" name="first_name" size="40" value=""></p>
<p>Last Name:
<input type="text" name="last_name" size="40" value=""></p>
<p>Email:
<input type="text" name="email" value=""></p>
<p>Headline:
<input type="text" name="headline" value=""></p>
<p>Summary:</br>
<textarea name="summary" rows="8" cols="80"></textarea></p>
<input type="hidden" name="profile_id" value="">
<p>Education:<input type="submit" id="addEdu" value="+">
<div id="edu_fields">
</div>
</p>
<p>Position: <input type="submit" id="addPos" value="+">
<div id="position_fields">
</div>
</p>
<p><input type="submit" value="Add">
<input type="submit" name="cancel" value="Cancel"></p>
</form>
</div>
<script>
countPos = 0;
countEdu = 0;
// http://stackoverflow.com/questions/17650776/add-remove-html-inside-div-using-javascript
$(document).ready(function(){
window.console && console.log('Document ready called');
$('#addPos').click(function(event){
// http://api.jquery.com/event.preventdefault/
event.preventDefault();
if ( countPos >= 9 ) {
alert("Maximum of nine position entries exceeded");
return;
}
countPos++;
window.console && console.log("Adding position "+countPos);
$('#position_fields').append(
'<div id="position'+countPos+'"> \
<p>Year: <input type="text" name="year'+countPos+'" value="" /> \
<input type="button" value="-" \
onclick="$(\'#position'+countPos+'\').remove();return false;"></p> \
<textarea name="desc'+countPos+'" rows="8" cols="80"></textarea>\
</div>');
});
$('#addEdu').click(function(event){
event.preventDefault();
if ( countEdu >= 9 ) {
alert("Maximum of nine education entries exceeded");
return;
}
countEdu++;
window.console && console.log("Adding education "+countEdu);
// Grab some HTML with hot spots and insert into the DOM
var source = $("#edu-template").html();
$('#edu_fields').append(source.replace(/@COUNT@/g,countEdu));
// Add the even handler to the new ones
$('.school').autocomplete({
source: "school.php"
});
});
$('.school').autocomplete({
source: "school.php"
});
});
</script>
</script>
<!-- HTML with Substitution hot spots -->
<script id="edu-template" type="text">
<div id="edu@COUNT@">
<p>Year: <input type="text" name="edu_year@COUNT@" value="" />
<input type="button" value="-" onclick="$('#edu@COUNT@').remove();return false;"><br>
<p>School: <input type="text" size="80" name="edu_school@COUNT@" class="school" value="" />
</p>
</div>
</script>
</body>
</html>