From a4bb441bc740494cd3925f965b13250a6b46b454 Mon Sep 17 00:00:00 2001 From: Natasha Wick Date: Tue, 31 Jul 2018 19:12:54 -0700 Subject: [PATCH 1/2] in progress --- ajaxCalls.js | 78 +++++++++++++++++++++++++++ index.php | 133 ++++++++++++++++++++++++++++++++++++++++++++++ postFunctions.php | 95 +++++++++++++++++++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 ajaxCalls.js create mode 100644 index.php create mode 100644 postFunctions.php diff --git a/ajaxCalls.js b/ajaxCalls.js new file mode 100644 index 0000000..041fa1d --- /dev/null +++ b/ajaxCalls.js @@ -0,0 +1,78 @@ + +function addNewProperty(){ + var address = $("#address").val() || ''; + var city = $("#city").val() || ''; + var state = $("#state").val() || ''; + var zip = $("#zip").val() || ''; + +//ADJUST THIS SO CAN ADD SALES AT SAME TIME + $.post('postFunctions.php', { do: 'addProperty', + address: address, + city: city, + state: state, + zip: zip }, function(id) { + if (id == 0) { + alert("That address already exists."); + } else{ + $('form').get(0).reset(); + var detailButton = ""; + var deleteButton = ""; + $('table').append(''+address+''+city+''+state+''+zip+''+detailButton+''+deleteButton+''); + } + }); +} + +function deleteProperty(property_id){ + $.post('postFunctions.php', { do: 'deleteProperty', + property_id: property_id}); + $('#'+property_id).remove(); +} + +function deleteSale(sale_id){ + $.post('postFunctions.php', { do: 'deleteSale', + sale_id: sale_id}); + $('#'+sale_id).remove(); +} + +function allInfo(property_id){ + //clear out the modal + $('form').get(0).reset(); + $('.sales').remove(); + + //populate with data + $.post('postFunctions.php', { do: 'allInfo', + property_id: property_id}, function(data){ + var allInfo = JSON.parse(data); + $('#address').val(allInfo[0]['address']); + $('#city').val(allInfo[0]['city']); + $('#state').val(allInfo[0]['state']); + $('#zip').val(allInfo[0]['zip']); + + if(allInfo[0]['sale_price'] || allInfo[0]['sale_date'] ){ + allInfo.forEach((sale, index) => { + var saleInputRows = "
"+ + ""+ + ""+ + "
"; + $('form').append(saleInputRows); + }); + } + + + + }); +} + +// +// //not an ajax call +// function addSaleRow(){ +// $("#addSaleRow").click(function(){ +// var saleInputRows = "
'+ +// ""+ +// ""; +//
'; +// $('form').append(saleInputRows); +// }); +// } + + diff --git a/index.php b/index.php new file mode 100644 index 0000000..a37bcf0 --- /dev/null +++ b/index.php @@ -0,0 +1,133 @@ +exec("USE nwick_php_test"); + +//if this is a new session, create the database and some test data +if(!isset($_SESSION['user'])){ + try { + $pdo = new PDO("mysql:host=localhost", $user, $password); + $pdo->exec("CREATE DATABASE IF NOT EXISTS nwick_php_test"); + $pdo->exec("USE nwick_php_test"); + + } catch (PDOException $e) {echo 'error'; + die("DB ERROR: ". $e->getMessage()); + } + + $pdo->exec("CREATE TABLE IF NOT EXISTS properties (property_id INT NOT NULL AUTO_INCREMENT, address VARCHAR(256), city VARCHAR(256), state VARCHAR(2), zip varchar(10), PRIMARY KEY (property_id))"); + $pdo->exec("CREATE TABLE IF NOT EXISTS sales (sale_id INT NOT NULL AUTO_INCREMENT, property_id INT, sale_date DATE, sale_price DECIMAL(10,2), PRIMARY KEY (sale_id))"); + + //simple test data + for($i = 0; $i<10; $i++){ + $insert = $pdo->prepare("INSERT INTO properties VALUES (0, :address, :city, :state, :zip);"); + $insert->execute(["address"=> "12".$i." Main St", "city"=>"San Diego", "state"=>"CA", "zip"=>"92101"]); + } + + $_SESSION['user'] = 'test_user'; +} + +$propertyTable = ""; +$propertyResults = $pdo->query("SELECT * FROM properties ORDER BY property_id"); + +while($row = $propertyResults->fetch() ) { + //don't display the id + $detailsButton = ""; + $deleteButton = ""; + $propertyTable .= ""; +} + +echo $propertyTable; +echo ""; + +?> + + + + + + Property Sales + + + + + + + + + + + + + + '); - } - }); + } + else{ + $('form').get(0).reset(); + var detailButton = ""; + var deleteButton = ""; + + if(existing_id != 0){ //refresh that row + $('#'+existing_id).get()[0].innerHTML = ''; + } + else{ //add it + $('table').append(''); + } + } + }); } function deleteProperty(property_id){ @@ -28,16 +50,21 @@ function deleteProperty(property_id){ $('#'+property_id).remove(); } -function deleteSale(sale_id){ - $.post('postFunctions.php', { do: 'deleteSale', - sale_id: sale_id}); - $('#'+sale_id).remove(); +function deleteSale(sale_id, inDB){ + if(inDB){ + $.post('postFunctions.php', { do: 'deleteSale', + sale_id: sale_id}); + $('#'+sale_id).remove(); + }else{ + $('#sale_'+sale_id).remove(); + } } +//get all the info for a specific property function allInfo(property_id){ //clear out the modal $('form').get(0).reset(); - $('.sales').remove(); + $('.sale-section').remove(); //populate with data $.post('postFunctions.php', { do: 'allInfo', @@ -47,32 +74,30 @@ function allInfo(property_id){ $('#city').val(allInfo[0]['city']); $('#state').val(allInfo[0]['state']); $('#zip').val(allInfo[0]['zip']); + $('#existing_id').val(property_id); if(allInfo[0]['sale_price'] || allInfo[0]['sale_date'] ){ allInfo.forEach((sale, index) => { - var saleInputRows = "
"+ - ""+ - ""+ + var saleInputRows = "
"+ + ""+ + ""+ + ""+ "
"; $('form').append(saleInputRows); }); - } - - - + } }); } -// -// //not an ajax call -// function addSaleRow(){ -// $("#addSaleRow").click(function(){ -// var saleInputRows = "
'+ -// ""+ -// ""; -//
'; -// $('form').append(saleInputRows); -// }); -// } +function addSaleRow(){ + var count = $('.sale-section').length; //how many so far in list + var saleInputRows = "
"+ + ""+ + ""+ + ""+ + "
"; + $('form').append(saleInputRows); + +} diff --git a/index.php b/index.php index a37bcf0..8faee85 100644 --- a/index.php +++ b/index.php @@ -73,10 +73,14 @@ @@ -108,6 +122,7 @@
+
AddressCityStateZip
$row[1]$row[2]$row[3]$row[4]$detailsButton$deleteButton
'+address+''+city+''+state+''+zip+''+detailButton+''+deleteButton+'
'+address+''+city+''+state+''+zip+''+detailButton+''+deleteButton+'
'+address+''+city+''+state+''+zip+''+detailButton+''+deleteButton+'