Variable returning Undefined

Asked

Viewed 77 times

0

I have this chunk of code and the inputs, when I click to edit the city and click to save works normal, now if I don’t change the city, it passes as Undefined and can not save. How can I do so when I do not change the city store the same way?

if ($street === $streetOriginal && $town === $townOriginal){

    $stmtSaveAddress = $conn->prepare("UPDATE address SET street = :street, number = :number, town = :town, complement = 
                                      :complement, city_id = (select city.city_id from city where city.link = :cityLink)
                                      WHERE address_id = :address");

    $stmtSaveAddress->bindValue(':street', $street);
    $stmtSaveAddress->bindValue(':number', ($number <= 0 ? NULL : $number));
    $stmtSaveAddress->bindValue(':town', $town);
    $stmtSaveAddress->bindValue(':cityLink', $city);
    $stmtSaveAddress->bindValue(':complement', $complement);
    $stmtSaveAddress->bindValue(':address', $addressId);


    echo 'city: '.$city;



    if ($stmtSaveAddress->execute()) {
        echo 'trueaddress';
    }else{
        echo 'falseaddress';

   }

Form:

<div class="inputs-without-icon" id="box-city">
   <input id="city" type="text" maxlength="100" placeholder="*Cidade" list="cities-list" value="<?php echo "{$Menu->getAddressCityName()} - {$Menu->getAddressState()}"; ?>" onkeyup="charByCharCity(this.value);">
   <input id="street" type="text" placeholder="*Rua" maxlength="70" value="<?php echo $Menu->getAddressStreet(); ?>">   
</div>
<datalist id="cities-list"><option data-number='0' value='Nome - Estado'>Cardápios</datalist>

This is my form.

  • What is Undefined and where?

  • the $city is coming as Undefined when I do not click on the input and change the city, but when I do not change should save the same way, ie remain the one that is already there.

  • Is it a checkbox? could put the form tbm.

  • 1

    @rray is an input, actually when I click on the input and type for example the letter C, it lists all cities with C, then select some and click save it saves normal. Now if I for example change the street and do not change the city it will not let insert. When I give an Alert in city it passes me Undefined when I do not change.

  • It seems to be a javascript problem, since the inputs have no name, so js should do some process before sending the information to php.

  • In js I take the values that are in the input and then vio ajax upload to my php file where I update. @rray

Show 1 more comment

2 answers

0

//Se quando vir undefined não é pra alterar o dado você faz assim:
if(isset($city)&&$city!=null){
   if ($stmtSaveAddress->execute()) {
       echo 'trueaddress';
   }else{
       echo 'falseaddress';
   }
}

0

You want to do the independent update if there has been a change in the data or not, correct?

In that case you just need to do the update without going through any logical test.

    $stmtSaveAddress = $conn->prepare("UPDATE address SET street = :street, number = :number, town = :town, complement = 
                                  :complement, city_id = (select city.city_id from city where city.link = :cityLink)
                                  WHERE address_id = :address");

$stmtSaveAddress->bindValue(':street', $street);
$stmtSaveAddress->bindValue(':number', ($number <= 0 ? NULL : $number));
$stmtSaveAddress->bindValue(':town', $town);
$stmtSaveAddress->bindValue(':cityLink', $city);
$stmtSaveAddress->bindValue(':complement', $complement);
$stmtSaveAddress->bindValue(':address', $addressId);


echo 'city: '.$city;



if ($stmtSaveAddress->execute()) {
    echo 'trueaddress';
}else{
    echo 'falseaddress';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.