Tengo dos archivos php, uno es el "form.php" donde tengo un formulario de envió con un campo desplegable (location) con 4 opciones a elegir.
Este funciona bien.
El otro archivo es el edit.php donde el usuario puede editar su anuncio, en este aparecen todos los campos del formulario anterior incluido el campo desplegable (location) con los datos que añadió.
El problema es que si realiza o no cambios y le da al botón de guardar, el formulario de edit.php detecta el campo (location) como vació, teniendo en cuenta que este campo le muestra una de las 4 opciones que eligio.
el campo location de edit.php es este.
Código PHP:
<p><label><b>Location </b> *(Options)<br />
<select name='location' id='location' class='postform' >
<option value="<?php if ($hfield == '3') { echo $errcolor; } ?>" ><?php echo get_post_meta($getmyad->ID, "location", true); ?> </option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
</select></label></p>
y el codigo entero de edit.php es este.
Código PHP:
<?php
/*
Name: Edit Ad Page
*/
$wpdb->hide_errors();
auth_redirect_login(); // if not logged in, redirect to login page
nocache_headers();
global $userdata;
get_currentuserinfo(); // grabs the user info and puts into vars
// check to see if the form has been posted. If so, validate the fields
if(!empty($_POST['submit']))
{
$title = trim($_POST['post_title']);
$price = cp_filter($_POST['price']);
$location = cp_filter($_POST['location']);
$phone = cp_filter($_POST['phone']);
$email = cp_filter($_POST['email']);
$name_owner = cp_filter($_POST['name_owner']);
$cp_adURL = cp_filter($_POST['cp_adURL']);
$description = trim($_POST['description']);
if($title == '')
{
$errmsg = __('Ad title cannot be blank','cp');
$hfield = '1';
}
else if($price == '')
{
$errmsg = __('Price cannot be blank','cp');
$hfield = '2';
}
else if($location == '')
{
$errmsg = __('Location cannot be blank','cp');
$hfield = '3';
}
else if($email == '')
{
$errmsg = __('Email address cannot be blank','cp');
$hfield = '5';
}
else if ( !cp_check_email($email) ) {
$errmsg = __('Email address is not valid','cp');
$hfield = '5';
}
else if($name_owner == '')
{
$errmsg = __('Name cannot be blank','cp');
$hfield = '6';
}
else if($description == '')
{
$errmsg = __('The description cannot be blank','cp');
$hfield = '7';
}
// if there are no errors, then process the ad updates
if($errmsg == '')
{
if ( get_option('filter_html') != "yes" ) {
$description = cp_filter($description);
}
// Put all post variables into an array
$my_ad = array();
$my_ad['ID'] = trim($_POST['ad_id']);
$my_ad['post_title'] = cp_filter($_POST['post_title']);
$my_ad['post_content'] = $description;
//Add or edit images
// First delete any images as posted
$image_folder_name = "folder-images";
$upload_arr = wp_upload_dir();
$imagedirectory = trailingslashit($upload_arr['basedir']) . $image_folder_name;
$delimages=$_POST['delimages'];
$noofdel=0;
$notdel=0;
$postedimages = explode(",", get_post_meta($my_ad['ID'], 'images', true));
$totalimages=count($postedimages);
if (!empty($delimages)){
foreach ($delimages as $imagekey=> $delimage){
if ($delimage!=""){
$imagetodelete = $imagedirectory.'/'.basename($postedimages[$delimage]);
$delstatus=unlink($imagetodelete);
if($delstatus=="1"){
$noofdel++;
// $listofdeleted=$delmessages.basename($postedimages[$delimage])." was deleted successfully.<br/>"; // can be used to see files being deleted
array_splice($postedimages,$delimage,1);
} else {
$notdel++;
// $listofnotdeleted=$delmessages.basename($postedimages[$delimage])." was not deleted<br/>"; // can be used to see files being deleted
}
}
}
$delmessages=$noofdel." images were deleted successfully<br/>".$listofdeleted;
if($notdel!=0){
$delmessages.=$notdel."images were not deleted<br/>".$listofnotdeleted;
}
}
// Second Insert new images
//1024 bytes = 1kb
//1024000 bytes = 1mb
// $image_folder_name = "folder-images";
$size_bytes = 1024000;
$size_mb = $size_bytes / 1024000;
$limitedext = array(".gif",".png",".jpg",".jpeg");
// http://codex.wordpress.org/Function_Reference/wp_upload_dir
// $upload_arr = wp_upload_dir();
// $dir_to_make = trailingslashit($upload_arr['basedir']) . $image_folder_name;
// $dir_to_make = "wp-content/uploads/folder-images";
$image_baseurl = trailingslashit($upload_arr['baseurl']) . $image_folder_name;
$image_name = substr(sanitize_title(alphanumericAndSpace($title)), 0, 20);
$i = rand();
$newimages = "";
$err2 = "";
while(list($key,$value) = each($_FILES['images']['name'])) {
if(!empty($value)) {
$filename = strtolower($value);
$filename = str_replace(" ", "-", $filename);
//get image extension
$tipul = strrchr($filename,'.');
$filename = $image_name."-$i".$tipul;
$add = "$imagedirectory/$filename";
$image = "$image_baseurl/$filename";
//$add = "$filename";
//Make sure that file size is correct
$file_size = $_FILES['images']['size'][$key]; //getting the right size that coresponds with the image uploaded
if ($file_size == "0"){
$err2 .= __('The file $value has 0 bytes.','cp') . "<br />";
} else {
if ($file_size > $size_bytes){
$err2 .= __('The file $value is bigger than 2MB.','cp') . "<br />";
}
}
//check file extension
$ext = strrchr($filename,'.');
if ( (!in_array(strtolower($ext),$limitedext)) ) {
$err2 .= __('The file $value is not an image.','cp') . "<br />";
}
//echo $_FILES['images']['type'][$key];
if ( $err2 == "" ) {
if (!file_exists($imagedirectory)) { mkdir($imagedirectory, 0777); }
copy($_FILES['images']['tmp_name'][$key], $add);
chmod("$add",0777);
//$images .= get_option('home')."/".$add.",";
$newimages .= $image . ",";
}
$err2 = "";
$i++;
}//if empty $value
}//end while
// Create new images array
array_merge($postedimages);
foreach ($postedimages as $image){
if ($image!=""){
$images=$images.$image.',';
}
}
// Update the ad in the db
wp_update_post( $my_ad );
// now go back and update the meta fields
update_post_meta($_POST['ad_id'], 'price', $price);
update_post_meta($_POST['ad_id'], 'location', $location);
update_post_meta($_POST['ad_id'], 'phone', $phone);
update_post_meta($_POST['ad_id'], 'email', $email);
update_post_meta($_POST['ad_id'], 'name', $name_owner);
update_post_meta($_POST['ad_id'], 'cp_adURL', $cp_adURL);
update_post_meta($_POST['ad_id'], 'images',$images.$newimages);
$cp_dashboard_url = $_POST['dashboard_url'];
$errmsg = '<div class="box-yellow"><b>' .$delmessages. __('Your ad has been updated!','cp') . '</b><br /><br /> <a href=".' . $cp_dashboard_url . '">' . __('My Dashboard ››','cp') . '</a></div>';
} else {
$errmsg = '<div class="box-red"><strong>** ' . $errmsg . ' **</strong></div>';
$errcolor = 'style="background-color:#FFEBE8;border:1px solid #CC0000;"';
}
}
// get the ad id from the querystring. Need this for the sql statement
$aid = $_GET["aid"];
$querystr = "SELECT wposts.* FROM $wpdb->posts wposts WHERE ID = $aid AND post_status <> 'draft' AND post_author = $userdata->ID";
// pull ad fields from db
$getmyad = $wpdb->get_row($querystr, OBJECT);
// start main edit page
require_once dirname( __FILE__ ) . '/form_process.php';
get_header();
include_classified_form();
?>
<style type="text/css">
.mid2 { border:1px solid #CCC; margin-bottom:10px; padding:5px; }
</style>
<script type="text/javascript">
function textCounter(field,cntfield,maxlimit) { if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit); else cntfield.value = maxlimit - field.value.length; }
</script>
<div class="content">
<div class="main ins">
<div class="left">
<div class="product">