...

View Full Version : Editing form



CoolAsCarlito
01-13-2011, 06:59 PM
I'm trying to figure out the best way to run about this.

Right now I have it set up so that two or more templates CAN NOT have the same name as it performs a check to verify that there aren't any matches.

Now problem here is that say I'm on the edit form and I'm editing a template.

Now I'm thinking what would be the next course of option because what if I only want to change the headercode but still keep the same name. When I submit it, its going to kickback my response that it can't be saved because there's already a template in there with that name even though I didn't change it.

Any suggestions?

processing page


if (isset($_POST['edittemplate'])) {

$templatename = mysqli_real_escape_string($dbc, $_POST['templatename']);
$headercode = mysqli_real_escape_string($dbc, $_POST['headercode']);
$footercode = mysqli_real_escape_string($dbc, $_POST['footercode']);
$templateID = (int)$_POST['templateID'];

$query = "SELECT * FROM `templates` WHERE `templatename` = '".$templatename."'";
$result = mysqli_query ( $dbc, $query ); // Run The Query
$rows = mysqli_num_rows($result);

if ($rows == 0) {

$query = "UPDATE `templates`
SET `templatename` = '".$templatename."', `headercode`='".$headercode."', `footercode`='".$footercode."' WHERE `id` = '".$templateID."'";

mysqli_query($dbc,$query);

echo "good";

} else {

echo "bad";

}
}


edit form page


$templateID = $_GET['id'];
$query = "SELECT
templates.templatename,
templates.headercode,
templates.footercode
FROM
templates
WHERE
templates.id = '" . $templateID . "'";
$result = mysqli_query ( $dbc, $query ); // Run The Query
$row = mysqli_fetch_array ( $result, MYSQL_ASSOC )
?>
<script type="text/javascript">
$(document).ready(function() {
$('div.message-error').hide();
$('div.message-success').hide();
$("input.submit").click(function() {
$('div.message-error').hide();
var templateID = $("input#templateID").val();
var templatename = $("input#templatename").val();
if (templatename == "") {
$("div.message-error").show();
$("input#templatename").focus();
return false;
}
var headercode = $("textarea#headercode").val();
if (headercode == "") {
$("div.message-error").show();
$("textarea#headercode").focus();
return false;
}
var footercode = $("textarea#footercode").val();
if (footercode == "") {
$("div.message-error").show();
$("textarea#footercode").focus();
return false;
}
var dataString = 'templatename=' + templatename + '&headercode=' + headercode + '&footercode=' + footercode + '&templateID=' + templateID + '&edittemplate=True';
$.ajax({
type: "POST",
url: "processes/template.php",
data: dataString,
success: function(myNewVar) {
if (myNewVar == 'good') {
$('div.message-error').hide();
$("div.message-success").html("<h6>Operation successful</h6><p>" + templatename + " was edited and saved successfully.</p>");
$("div.message-success").show().delay(10000).hide("slow");
$('#content').load('mods/templates.php');
} else {
$('div.message-success').hide();
$("div.message-error").html("<h6>Operation unsuccessful</h6><p>" + templatename + " already exists in the database.</p>");
$("div.message-error").show();
}
}
});
return false;
});
});
</script>
<!-- Form -->
<form action="#" id="templateform" >
<fieldset>
<legend>Edit Template</legend>
<div class="field required">
<label for="templatename">Template Name</label>
<input type="text" class="text" name="templatename" id="templatename" title="Template Name" value="<?php echo $row['templatename']; ?>"/>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="headercode">Header Code</label>
<textarea name="headercode" id="headercode" title="Header Code"><?php echo $row['headercode']; ?></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="footercode">Footer Code</label>
<textarea name="footercode" id="footercode" title="Footer Code"><?php echo $row['footercode']; ?></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<input type="hidden" id="templateID" value="<?php echo $templateID; ?>" />
<input type="submit" class="submit" name="edittemplate" id="edittemplate" title="Submit Template" value="Submit Template"/>
</fieldset>
</form>
<!-- /Form -->
<!-- Messages -->
<div class="message message-error">
<h6>Required field missing</h6>
<p>Please fill in all required fields. </p>
</div>
<div class="message message-success">
<h6>Operation succesful</h6>
<p>Template was edited and saved to the database.</p>
</div>
<!-- /Messages -->

Fumigator
01-13-2011, 07:11 PM
What is the unique primary key of this templates table? Use it in your duplicate checking to only enforce the name uniqueness when the table being edited is different from the table found in the duplicate check.

For example, let's say you have a template_id for the primary key. Store this template_id, either in the $_SESSION array (I don't know if you're using sessions) or just as a hidden form input element. Then add it to your duplicate check:



SELECT *
FROM `templates`
WHERE `templatename` = '$templatename'
AND template_id != $templateIdYouStoredInTheForm


p.s. you know you need to be checking for query errors after you call mysql_query(), right? ;) :rolleyes:

CoolAsCarlito
01-13-2011, 07:48 PM
Okay so its getting there.

I went into one of the templates to edit it on the form and IT DID allow me to change the header and footer codes just as a test and it changed it in the database after form submission however it still returned the myNewVar as being bad and gave me the error message even though it did work.

edit form


<script type="text/javascript">
$(document).ready(function() {
$('div.message-error').hide();
$('div.message-success').hide();
$("input.submit").click(function() {
$('div.message-error').hide();
var templatename = $("input#templatename").val();
if (templatename == "") {
$("div.message-error").show();
$("input#templatename").focus();
return false;
}
var headercode = $("textarea#headercode").val();
if (headercode == "") {
$("div.message-error").show();
$("textarea#headercode").focus();
return false;
}
var footercode = $("textarea#footercode").val();
if (footercode == "") {
$("div.message-error").show();
$("textarea#footercode").focus();
return false;
}
var dataString = 'templatename=' + templatename + '&headercode=' + headercode + '&footercode=' + footercode + '&submittemplate=True';
$.ajax({
type: "POST",
url: "processes/template.php",
data: dataString,
success: function(myNewVar) {
if (myNewVar == 'good') {
$('div.message-error').hide();
$("div.message-success").html("<h6>Operation successful</h6><p>" + templatename + " was saved successfully.</p>");
$("div.message-success").show().delay(10000).hide("slow");
$(':input','#templateform')
.not(':submit')
.val('')
} else {
$('div.message-success').hide();
$("div.message-error").html("<h6>Operation unsuccessful</h6><p>" + templatename + " already exists in the database.</p>");
$("div.message-error").show();
}
}
});
return false;
});
});
</script>
<!-- Form -->
<form action="#" id="templateform" >
<fieldset>
<legend>Add New Template</legend>
<div class="field required">
<label for="templatename">Template Name</label>
<input type="text" class="text" name="templatename" id="templatename" title="Template Name" />
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="headercode">Header Code</label>
<textarea name="headercode" id="headercode" title="Header Code"></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="footercode">Footer Code</label>
<textarea name="footercode" id="footercode" title="Footer Code"></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<input type="submit" class="submit" name="submittemplate" id="submittemplate" title="Submit Template" value="Submit Template"/>
</fieldset>
</form>
<!-- /Form -->
<!-- Messages -->
<div class="message message-error">
<h6>Required field missing</h6>
<p>Please fill in all required fields. </p>
</div>
<div class="message message-success">
<h6>Operation succesful</h6>
<p>Template was added to the database.</p>
</div>
<!-- /Messages -->
<?php
break;

case 'edit':
$templateID = $_GET['id'];
$query = "SELECT
templates.templatename,
templates.headercode,
templates.footercode
FROM
templates
WHERE
templates.id = '" . $templateID . "'";
$result = mysqli_query ( $dbc, $query ); // Run The Query
$row = mysqli_fetch_array ( $result, MYSQL_ASSOC )
?>
<script type="text/javascript">
$(document).ready(function() {
$('div.message-error').hide();
$('div.message-success').hide();
$("input.submit").click(function() {
$('div.message-error').hide();
var templateID = $("input#templateID").val();
var templatename = $("input#templatename").val();
if (templatename == "") {
$("div.message-error").show();
$("input#templatename").focus();
return false;
}
var headercode = $("textarea#headercode").val();
if (headercode == "") {
$("div.message-error").show();
$("textarea#headercode").focus();
return false;
}
var footercode = $("textarea#footercode").val();
if (footercode == "") {
$("div.message-error").show();
$("textarea#footercode").focus();
return false;
}
var dataString = 'templatename=' + templatename + '&headercode=' + headercode + '&footercode=' + footercode + '&templateID=' + templateID + '&edittemplate=True';
$.ajax({
type: "POST",
url: "processes/template.php",
data: dataString,
success: function(myNewVar) {
if (myNewVar == 'good') {
$('div.message-error').hide();
$("div.message-success").html("<h6>Operation successful</h6><p>" + templatename + " was edited and saved successfully.</p>");
$("div.message-success").show().delay(10000).hide("slow", function() {
$('#content').load('mods/templates.php');
});
} else {
$('div.message-success').hide();
$("div.message-error").html("<h6>Operation unsuccessful</h6><p>" + templatename + " already exists in the database.</p>");
$("div.message-error").show();
}
}
});
return false;
});
});
</script>
<!-- Form -->
<form action="#" id="templateform" >
<fieldset>
<legend>Edit Template</legend>
<div class="field required">
<label for="templatename">Template Name</label>
<input type="text" class="text" name="templatename" id="templatename" title="Template Name" value="<?php echo $row['templatename']; ?>"/>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="headercode">Header Code</label>
<textarea name="headercode" id="headercode" title="Header Code"><?php echo $row['headercode']; ?></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="footercode">Footer Code</label>
<textarea name="footercode" id="footercode" title="Footer Code"><?php echo $row['footercode']; ?></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<input type="hidden" id="templateID" value="<?php echo $templateID; ?>" />
<input type="submit" class="submit" name="edittemplate" id="edittemplate" title="Submit Template" value="Submit Template"/>
</fieldset>
</form>
<!-- /Form -->
<!-- Messages -->
<div class="message message-error">
<h6>Required field missing</h6>
<p>Please fill in all required fields. </p>
</div>
<div class="message message-success">
<h6>Operation succesful</h6>
<p>Template was edited and saved to the database.</p>
</div>
<!-- /Messages -->


process page


if (isset($_POST['edittemplate'])) {

$templatename = mysqli_real_escape_string($dbc, $_POST['templatename']);
$headercode = mysqli_real_escape_string($dbc, $_POST['headercode']);
$footercode = mysqli_real_escape_string($dbc, $_POST['footercode']);
$templateID = (int)$_POST['templateID'];

$query = "SELECT * FROM `templates` WHERE `templatename` = '".$templatename."' AND id != '".$templateID."'";
$result = mysqli_query ( $dbc, $query ); // Run The Query
$rows = mysqli_num_rows($result);

if ($rows == 0) {

$query = "UPDATE `templates`
SET `templatename` = '".$templatename."', `headercode`='".$headercode."', `footercode`='".$footercode."' WHERE `id` = '".$templateID."'";

mysqli_query($dbc,$query);

echo "good";

} else {

echo "bad";

}
}

Fumigator
01-13-2011, 08:43 PM
Debugging step #1: Put in an alert to see what the value of myNewVar is. (Posting to CF should be step #3 or #4 :D)

CoolAsCarlito
01-13-2011, 08:55 PM
When I go in and and change either the headercode or footercode for one fo the templates and click the submit buttton it brings back the error message that there is already a template with that name in the db but it still manages to update the template with my new input even though I didn't change the name.

When I change the name to something that IS NOT in the db it still comes back and show the error message that there is a already a template in the database with that name even though there is NOT and STILL changes the name.

That should come what's going on.

CoolAsCarlito
01-13-2011, 08:57 PM
Oddly enough its still reporting back with what should be the correct value of myNewVar

CoolAsCarlito
01-13-2011, 11:45 PM
I'm not sure what I did but now the template works but I have a similar problem for my content pages. When you create a new content page even if no other content pages are in there it'll insert but itll give you the error saying there's already one in there.

THe query is a little different seeing as there is more than 1 WHERE clause separated with an OR.

It shouldn't make a different but keep in mind the above was an edit form and this is my add form which is all the same but doing different things.

Add Form


<script type="text/javascript">
$(document).ready(function() {
$('div.message-error').hide();
$('div.message-success').hide();
$("input.submit").click(function() {
$('div.message-error').hide();
var contentpage = $("input#contentpage").val();
if (contentpage == "") {
$("div.message-error").show();
$("input#contentpage").focus();
return false;
}
var shortname = $("input#shortname").val();
if (shortname == "") {
$("div.message-error").show();
$("input#shortname").focus();
return false;
}
var template = $("select#template").val();
if (template == "") {
$("div.message-error").show();
$("select#template").focus();
return false;
}
var linebreak = $("select#linebreak").val();
if (linebreak == "") {
$("div.message-error").show();
$("select#linebreak").focus();
return false;
}
var backlink = $("select#backlink").val();
if (backlink == "") {
$("div.message-error").show();
$("select#backlink").focus();
return false;
}
var showheading = $("select#showheading").val();
if (showheading == "") {
$("div.message-error").show();
$("select#showheading").focus();
return false;
}
var visible = $("select#visible").val();
if (visible == "") {
$("div.message-error").show();
$("select#visible").focus();
return false;
}
var contentcode = $("textarea#contentcode").val();
if (contentcode == "") {
$("div.message-error").show();
$("textarea#contentcode").focus();
return false;
}
var dataString = 'contentpage=' + contentpage + '&shortname=' + shortname + '&template=' + template + '&linebreak=' + linebreak + '&backlink=' + backlink + '&showheading=' + showheading + '&visible=' + visible + '&contentcode=' + contentcode + '&submitcontentpage=True';
$.ajax({
type: "POST",
url: "processes/contentpage.php",
data: dataString,
success: function(myNewVar) {
if (myNewVar == 'good') {
$('div.message-error').hide();
$("div.message-success").html("<h6>Operation successful</h6><p>" + contentpage + " Page saved successfully.</p>");
$("div.message-success").show().delay(10000).hide("slow");
$(':input','#contentpageform')
.not(':submit')
.val('')
} else {
$('div.message-success').hide();
$("div.message-error").html("<h6>Operation unsuccessful</h6><p>" + contentpage + " already exists in the database.</p>");
$("div.message-error").show();
}
}
});
return false;
});
});
</script>
<!-- Form -->
<form action="#" id="contentpageform" >
<fieldset>
<legend>Add New Content Page</legend>
<div class="field required">
<label for="contentpage">Content Page Name</label>
<input type="text" class="text" name="contentpage" id="contentpage" title="Content Page Name"/>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="shortname">Content Page Short Name</label>
<input type="text" class="text" name="shortname" id="shortname" title="Content Page Short Name"/>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="template">Template</label>
<select class="dropdown" name="template" id="template" title="Template">
<option value="0">- Select -</option>
<?php
$query = 'SELECT id, templatename FROM templates';
$result = mysqli_query ( $dbc, $query ); // Run The Query
while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
print "<option value=\"".$row['id']."\">".$row['templatename']."</option>\r";
}
?>
</select>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="linebreak">Line Break</label>
<select class="dropdown" name="linebreak" id="linebreak" title="Line Break">
<option value="0">- Select -</option>
<?php
$linebreak = array('Yes', 'No');
foreach($linebreak as $lb):
?>
<option value="<?php echo $lb; ?>"><?php echo $lb; ?></option>

<?php endforeach; ?>
</select>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="backlink">Back Link</label>
<select class="dropdown" name="backlink" id="backlink" title="Back Link">
<option value="0">- Select -</option>
<?php
$backlink = array('Yes', 'No');
foreach($backlink as $bl):
?>
<option value="<?php echo $bl; ?>"><?php echo $bl; ?></option>

<?php endforeach; ?>
</select>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="showheading">Show Heading</label>
<select class="dropdown" name="showheading" id="showheading" title="Show Heading">
<option value="0">- Select -</option>
<?php
$showhead = array('Yes', 'No');
foreach($showhead as $sh):
?>
<option value="<?php echo $sh; ?>"><?php echo $sh; ?></option>

<?php endforeach; ?>
</select>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="visible">Visible</label>
<select class="dropdown" name="Visible" id="visible" title="Visible">
<option value="0">- Select -</option>
<?php
$visible = array('Yes', 'No');
foreach($visible as $vis):
?>
<option value="<?php echo $vis; ?>"><?php echo $vis; ?></option>

<?php endforeach; ?>
</select>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<div class="field required">
<label for="contentcode">Content Code</label>
<textarea name="contentcode" id="contentcode" title="Content Code"></textarea>
<span class="required-icon tooltip" title="Required field - This field is required, it cannot be blank, and must contain something that is different from emptyness in order to be filled in. ">Required</span>
</div>
<input type="submit" class="submit" name="submitcontentpage" id="submitcontentpage" title="Submit Content Page" value="Submit Content Page"/>
</fieldset>
</form>
<!-- /Form -->
<!-- Messages -->
<div class="message message-error">
<h6>Required field missing</h6>
<p>Please fill in all required fields. </p>
</div>
<div class="message message-success">
<h6>Operation succesful</h6>
<p>Content Page was added to the database.</p>
</div>
<!-- /Messages -->



Process Page


if (isset($_POST['submitcontentpage'])) {

$contentpage = mysqli_real_escape_string($dbc, $_POST['contentpage']);
$shortname = mysqli_real_escape_string($dbc, $_POST['shortname']);
$contentcode = mysqli_real_escape_string($dbc, $_POST['contentcode']);
$linebreak = mysqli_real_escape_string($dbc, $_POST['linebreak']);
$backlink = mysqli_real_escape_string($dbc, $_POST['backlink']);
$showheading = mysqli_real_escape_string($dbc, $_POST['showheading']);
$visible = mysqli_real_escape_string($dbc, $_POST['visible']);
$template = mysqli_real_escape_string($dbc, $_POST['template']);

$query = "SELECT * FROM `contentpages` WHERE (`contentpage` = '".$contentpage."') OR (`shortname` = '".$shortname."')";
$result = mysqli_query ( $dbc, $query ); // Run The Query
$rows = mysqli_num_rows($result);
echo $query;
echo $rows;

if ($rows == 0) {

$query = "INSERT INTO `contentpages`
(contentpage, shortname, contentcode, linebreaks, backlink, showheading, visible, template_id, creator_id, datecreated)
VALUES
('".$contentpage."','".$shortname."','".$contentcode."','".$linebreak."','".$backlink."','".$showheading."','".$visible."','".$template."', 1, NOW())";

mysqli_query($dbc,$query);

echo "good";

} else {

echo "bad";

}

}

Fumigator
01-14-2011, 04:27 PM
The logic concept is the same. Follow your usual debugging process to find the problem.

CoolAsCarlito
01-14-2011, 08:39 PM
The odd thing is that it responds with good but still shows an error message. Doesn't make sense.

Fumigator
01-14-2011, 09:24 PM
No, it doesn't, and without a test page I can get to there's nothing I can do to help on this side.

CoolAsCarlito
01-16-2011, 02:55 AM
Topic is resolved. It won't let me edit the post so I can make it resolved.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum