John
John

Reputation: 59

If statement only executing the first condition and running others but no results

I am using the following php code to run some queriers against my database.

if ($Name !="" && $newName !=""){ 
    $sql = "UPDATE PRODUCTS SET P_NAME = '$newName' WHERE P_Name = '$Name'";
    $conn->exec($sql);
    echo 'name was run';
}
if($Name !="" && $Description !=""){
    $sql = "UPDATE PRODUCTS SET P_Description = '$Description' WHERE P_NAME = '$Name'";
    $conn->exec($sql); 
    echo 'des worked';
}
if($Name !="" && $Price !=""){
    $sql = "UPDATE PRODUCTS SET P_Price = '$Price' WHERE P_NAME = '$Name'";
    $conn->exec($sql);
    echo 'price worked';
}
if ($Name !="" && $newQuant!=""){
    $sql = "UPDATE PRODUCTS SET P_Quantity = '$newQuant' WHERE P_NAME = '$Name'";
    $conn->exec($sql);
    echo 'quant worked';
}

when i use this in the form using action= all the updates work taking the values from the form and using them in the code. However I am now trying to use the following javascript code to insert the data but it is only taking the first condition and working. The else if conditions run with no error but do not pass the data through. Can anyone see why this is?

function editData(){
    xmlhttp = new XMLHttpRequest();
    var name = document.getElementById("orginalName");
    var newName = document.getElementById("newName");
    var Description = document.getElementById("newDescription");
    var Price = document.getElementById("newPrice");
    var Quant = document.getElementById('newQuantity');
    if (name.value !="" && newName.value !="" ){
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&newName=" + newName.value);
    }
    else if (name.value !="" && Description.value !=""){
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&Description=" + Description.value);
    }
    else if (name.value !="" && Price.value !=""){
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&Price=" + Price.value);
    }
    else if (name.value !="" && Quant.value !=""){
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&newQuant=" + Quant.value);
    }
    else{
        alert("please enter some data");
    }
}

submitButton = document.getElementById("editButton");
submitButton.addEventListener("click", editData);

below is the form the JS uses:

<form id="dataForm" method="post">
  <h2 id="formheader"> Edit Product</h2>
    <div>
      <label>Product Name:</label>
      <input class="inputForm" id="orginalName" type="text" name="Name">
    </div>
    <div>
      <label>New Name:</label>
      <input class="inputForm" id="newName" type="text" name="newName">
    </div>
    <div>
      <label>New Description:</label>
      <input class="inputForm" id="newDescription" type="text" name="description">
    </div>
    <div>
      <label>New Price:</label>
      <input class="inputForm" id="newPrice" type="text" name="Price">
    </div>
    <div>
    <label>New Quantity:</label>
    <input class="inputForm" id="newQuantity" type="text" name="quantity">
  </div> 
    <div id="theSubmit">
      <button id="editButton">Submit</button>
    </div>
  </form>
</section>

Upvotes: 0

Views: 2483

Answers (1)

collapsar
collapsar

Reputation: 17238

Your JS code uses a cascaded if, while your php code consists of individual conditionals processed one after another.

Remove all else keywords. The last else branch will need a different guard.

So your JS code should look like this:

    //..
    var Quant = document.getElementById('newQuantity');
    var data_seen = false;
        // this is a flag to record whether any data has been seen. Used in the guard ofthe alert statement.
    if (name.value !="" && newName.value !="" ){
        data_seen = true;
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&newName=" + newName.value);
    }
    if (name.value !="" && Description.value !=""){
        data_seen = true;
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&Description=" + Description.value);
    }
    if (name.value !="" && Price.value !=""){
        data_seen = true;
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&Price=" + Price.value);
    }
    if (name.value !="" && Quant.value !=""){
        data_seen = true;
        xmlhttp.open("POST","editForm.PHP",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp.send("Name=" + name.value + "&newQuant=" + Quant.value);
    }
    if (!data_seen) {
        alert("please enter some data");
    }
    //...

Upvotes: 1

Related Questions