I have a section within a large form where a user can add new user-inputted fields to the form to be filled in. For an example see the jsfiddle(jsfiddle.net/4gdJk/3/) which shows the section I am referring to. The view here is a partial render of another models view file. The data needs to be saved to another model via ajax, on addition or removals of the rows.
I currently am using ajax for the validation of the pages model elements, but this validation is not being done on this specific section as it is not the same model. My question then is using ajax, how should I validate and then save the elements posed here? The data does not need to save live, only on submission, but validation needs to be performed via ajax to another model, and I have no clue how I should be saving this data when the form is saved as some data may already be present which can be modified or removed, therefore some data from the database needs to be removed or added, or even modified. I do not know how to do this using Yii. I don’t exactly want to work around the framework to pull this off, but I do not know how to do it using the framework.
As I still am looking for ways to do this via Yii framework I am posting again. I have done this a slightly workaround way using custom jQuery which makes ajax calls to a file that really has nothing Yii related in it. Here is the contents of the file that is being called via ajax:
<?php
//This file is used to add, remove and update an items service monitoring.
include_once(dirname(__FILE__).'/protected/config/main.php');
//Adds service monitoring for an item.
function add() {
$db = new mysqli(INVENTORY_HOST, INVENTORY_USER, INVENTORY_PASSWORD, INVENTORY_DB);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$pkey = $db->escape_string($_POST['pkey']);
$serviceName = $db->escape_string($_POST['serviceName']);
$monitor = $_POST['monitor'];
if($serviceName == 'null' || trim($serviceName)=='')
die('You cannot add a service with no name!');
$sql = "INSERT INTO `services`(`ID`, `Host`, `ServiceName`, `Monitored`, `Status`) VALUES (NULL, '$pkey','$serviceName',$monitor,NULL)";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
else {
echo $serviceName. ' service added successfully';
}
}
//Deletes service monitoring for an item
function delete() {
$db = new mysqli(INVENTORY_HOST, INVENTORY_USER, INVENTORY_PASSWORD, INVENTORY_DB);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$pkey = $db->escape_string($_POST['pkey']);
$serviceName = $db->escape_string($_POST['serviceName']);
$sql = "DELETE FROM services WHERE Host='$pkey' AND ServiceName='$serviceName'";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
else {
echo $serviceName. ' service removed successfully.';
}
}
//Updates whether or not an item should be monitored.
function update() {
$db = new mysqli(INVENTORY_HOST, INVENTORY_USER, INVENTORY_PASSWORD, INVENTORY_DB);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$pkey = $db->escape_string($_POST['pkey']);
$serviceName = $db->escape_string($_POST['serviceName']);
$monitor = $_POST['monitor'];
$sql = "UPDATE services SET Monitored='$monitor' WHERE Host='$pkey' AND ServiceName='$serviceName'";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
else {
echo $serviceName. ' service updated successfully.';
}
}
//Handles incomming post request which should be from an ajax call. Based on the given action the request is handled.
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'add' : add();break;
case 'delete' : delete();break;
case 'update' : update();break;
}
}
?>
It probably is more ideal to use Yii, but I’m afraid I don’t know how it should be setup correctly to work with this kind of configuration.