diff --git a/ajaxCalls.js b/ajaxCalls.js new file mode 100644 index 0000000..d0f007c --- /dev/null +++ b/ajaxCalls.js @@ -0,0 +1,103 @@ +//handles adding new property and editing existing ones +function addNewProperty(){ + var address = $("#address").val() || ''; + var city = $("#city").val() || ''; + var state = $("#state").val() || ''; + var zip = $("#zip").val() || ''; + var existing_id = $("#existing_id").val() || 0; + var salesAndDates= [[]]; + var count = $('.sale-section').length; //how many so far in list + var price, date; + + for(var i=0; iView Details"; + var deleteButton = ""; + + if(existing_id != 0){ //refresh that row + $('#'+existing_id).get()[0].innerHTML = ''+address+''+city+''+state+''+zip+''+detailButton+''+deleteButton+''; + } + else{ //add it + $('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, 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(); + $('.sale-section').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']); + $('#existing_id').val(property_id); + + if(allInfo[0]['sale_price'] || allInfo[0]['sale_date'] ){ + allInfo.forEach((sale, index) => { + 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 new file mode 100644 index 0000000..8faee85 --- /dev/null +++ b/index.php @@ -0,0 +1,148 @@ +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 + + + + + + + + + + + + + +
AddressCityStateZip
$row[1]$row[2]$row[3]$row[4]$detailsButton$deleteButton