Question
Modify/add jQuery scripts in form_viz.php, so that the input data is posted to the insert.php file when the submit button is clicked. Then, modify the
Modify/add jQuery scripts in "form_viz.php", so that the input data is posted to the "insert.php" file when the "submit" button is clicked.
Then, modify the "insert.php" file so that the posted data can be inserted in to the "city" table of the "sakila" DB as an entry.
This is the code I have so far for "form_viz.php"
svg {
background-color: lightgray;
float: left;
}
.bar {
fill: blue;
}
.bar:hover {
fill: yellow;
}
.text {
fill: white;
font-family: sans-serif
}
#message {
background: yellow;
max-width: 500px;
margin: 10px;
}
#submit {
width: 300px;
height: 50px;
font-size: 20px;
margin: 10px;
}
#city_form div {
border: 1px solid gray;
max-width: 900px;
}
$(document).ready(function(){
// bar chart
// Data
// var dataArray = [];
var svg = d3.select(".bar");
d3.csv("query.php", function(error, dataArray) {
if (error) throw error;
console.log(dataArray);
// Creating bars
svg.selectAll("rect")
.data(dataArray)
.enter()
.append("rect")
.attr("class", "bar")
.attr("height", function(d, i) {return (d.num_customers * 5)})
.attr("width","10")
.attr("x", function(d, i) {return (i * 10) + 25})
.attr("y", function(d, i) {return 500 - (d.num_customers * 5)});
// Creating numbers for each bar
svg.selectAll("text")
.data(dataArray)
.enter()
.append("text")
.text(function(d) {return d.num_customers})
.attr("class", "text")
.attr("x", function(d, i) {return (i * 10) + 25})
.attr("y", function(d, i) {return 500 - (d.num_customers * 5)});
});
$("#submit").click(function(){
// for now, these variables are fake. You need to get these data from the form.
city_name = "test";
country_id = 0;
$.post("insert.php",
{
city_name: city_name,
country_id: country_id
}, function(data, status){
$("#city_form").trigger('reset');
$("#message").html(data).fadeIn(2000).delay(3000).fadeOut(2000);
}
);
});
});
And here is my code for "inset.php"
require "config.php";
// Create connection $conn = mysqli_connect($server, $username, $password, $db);
// Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
// A function for general queries. function query_to_db($conn, $sql){ $result = mysqli_query($conn, $sql);
if ($result) { echo "Your query was successful"; } else { echo "Error: " . $sql . " " . mysqli_error($conn); }
}
// Receiving the submitted data
// You need to save the data into the database. Write an INSERT query here. $sql = ""; query_to_db($conn, $sql);
mysqli_close($conn);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started