kie kie - 24 days ago 8
PHP Question

disabled not submitting in php

I have a select box in my form and inside that select box there's a list of a,b,c and others.and I have a input type="text" which is disabled.now, when I select others in select box the input will be enabled so that the user can type anything that cannot be found in the select item.this function works fine but, the only problem is that when I try it now to update its not submitting..can anyone help me..

html

<Select name="category" id="category" class="form-control" onChange="category();">
<option value="<?php echo $a; ?>" selected="selected">Please Select a Category</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<option value="OTHERS">Other/s</option>
</select>

<input type="text" id="Others" name="Others" value="<?php echo $Others; ?>" class="form-control" placeholder="Other/s" disabled="true">


javascript

$(function(){
$("#category").change(function(){
if($(this).val()=="OTHERS"){
console.log(true);
$("#Others").removeAttr("disabled");
}
else{
console.log(false);
$("#Others").attr("disabled", "disabled");
}
});
});


php

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$ID = $_GET['ID'];
$Category = $_POST['Category'];
$Others = $_POST['Others'];

if($LTS->update($ID,$Category,$Others)){
echo "<script type='text/javascript'>alert('Successfully Updated!');</script>";
}
else {
echo "<script type='text/javascript'>alert('Updating Failed!');</script>";
}
}

if(isset($_GET['ID'])) {
$ID = $_GET['ID'];
extract($LTS->getID($ID));
}
?>


class.user.php

public function update($ID,$Category,$Others) {
try{
$stmt = $this->db->prepare("UPDATE sfund SET Category = :Categoryy,Others = :Others WHERE ID = :ID");
$stmt->bindparam(":Category", $Category);
$stmt->bindparam(":Others", $Others);

$stmt->bindparam(":ID", $ID);
$stmt->execute();
return true;
}
catch(PDOException $e) {
echo $e->getMessage();
return false;
}
}

Answer
$('#submitButton').click(function() {
  var others = $('#Others').val();
  var category = $('#category').val();
  $.post('script.php', {
    action: 'submit',
    others: others,
    cat: category
  });
});

script.php is the name of your php file. include the path if it is inside the folder.
In your PHP, change $_SERVER["REQUEST_METHOD"] == "POST" with
isset($_POST['action']) && $_POST['action'] == "submit" to separate it from other ajax that you will create.