PHP and Javascript issue. Function return always the last value












-1















I searched this website and couldn't find an answer.
I have a php page that display a dropdown list with 2 choices. When on change, a Javascript execute a function that opens a php page to update my database, sending the ID and another parameter with the URL. The problem is the ID value is always the same (the last of the list). Can you help me please, I am very new in PHP and Javascript. Thank you in advance. Here is the PHP code and the javascript:






	<?php
$AvailabilityCondition = "1=1";

if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}


$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";



if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;

?>






<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >

<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>

</form>

<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>



<!-- **************************************************************************************************************** -->
</td>
<td>
<a href="rental-delete.php?id=<?php echo ($rowrentals["id"])?>" onclick="return confirm('Are you sure? This CAN NOT be undone!')">!!! DELETE !!!</a>
</td>
</tr>
</table>
</div>


<?php
}}



/* free result set */
$rentalresult->free();

/* close connection */
$link->close();
?>

<br/><br/>
<div align="center">
<a href="index.php"><b>Back to Managers Main Menu</b></a>
</div>

</body> ​





I just posted more of my PHP page. So you can see the query and the WHILE, where $rowrentals["id"] is coming from.



Here is also a screen capture of how the page looks like: screen capture



I echoed the $rowrentals["id"] under each availability dropdown.
But whatever row I chose to change the availability, it always pass Id=9, the last one. That is what confuses me.



Before the screen capture, all five rows where "Available". Then I selected "Rented" on the first row. The page updated and since Id always =9, you can see the last row "Rented".



The Javascript is working to retrieve the value of the selected item. Because it opens this page perfectly: status-update.php?rented=Yes&Id=9
But again, Id is always 9...










share|improve this question

























  • If i understand your question correctly you need to check the selected value not the value see this example var optionSelected = $("option:selected", this); VS var valueSelected = this.value;`

    – Puya Sarmidani
    Nov 21 '18 at 19:42













  • Possible duplicate of Retrieving the text of the selected <option> in <select> element

    – basic
    Nov 21 '18 at 19:48











  • As Puya described you need to get the value of the selected option. See this SO article: stackoverflow.com/questions/1085801/…

    – J. Schmale
    Nov 21 '18 at 19:51











  • Can you at least clarify where the problem exactly is? If there is some problem reading out a selected item in the browser, the problem is in no way related to PHP. If the markup is not generated properly, the problem is in no way related to JS

    – Nico Haase
    Nov 21 '18 at 19:51











  • Why are you doing document.getElementById ('availabilityChoice').innerHTML = val;? Why are you trying to replace the inner HTML of the select element with its value?

    – Alon Eitan
    Nov 21 '18 at 19:57
















-1















I searched this website and couldn't find an answer.
I have a php page that display a dropdown list with 2 choices. When on change, a Javascript execute a function that opens a php page to update my database, sending the ID and another parameter with the URL. The problem is the ID value is always the same (the last of the list). Can you help me please, I am very new in PHP and Javascript. Thank you in advance. Here is the PHP code and the javascript:






	<?php
$AvailabilityCondition = "1=1";

if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}


$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";



if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;

?>






<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >

<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>

</form>

<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>



<!-- **************************************************************************************************************** -->
</td>
<td>
<a href="rental-delete.php?id=<?php echo ($rowrentals["id"])?>" onclick="return confirm('Are you sure? This CAN NOT be undone!')">!!! DELETE !!!</a>
</td>
</tr>
</table>
</div>


<?php
}}



/* free result set */
$rentalresult->free();

/* close connection */
$link->close();
?>

<br/><br/>
<div align="center">
<a href="index.php"><b>Back to Managers Main Menu</b></a>
</div>

</body> ​





I just posted more of my PHP page. So you can see the query and the WHILE, where $rowrentals["id"] is coming from.



Here is also a screen capture of how the page looks like: screen capture



I echoed the $rowrentals["id"] under each availability dropdown.
But whatever row I chose to change the availability, it always pass Id=9, the last one. That is what confuses me.



Before the screen capture, all five rows where "Available". Then I selected "Rented" on the first row. The page updated and since Id always =9, you can see the last row "Rented".



The Javascript is working to retrieve the value of the selected item. Because it opens this page perfectly: status-update.php?rented=Yes&Id=9
But again, Id is always 9...










share|improve this question

























  • If i understand your question correctly you need to check the selected value not the value see this example var optionSelected = $("option:selected", this); VS var valueSelected = this.value;`

    – Puya Sarmidani
    Nov 21 '18 at 19:42













  • Possible duplicate of Retrieving the text of the selected <option> in <select> element

    – basic
    Nov 21 '18 at 19:48











  • As Puya described you need to get the value of the selected option. See this SO article: stackoverflow.com/questions/1085801/…

    – J. Schmale
    Nov 21 '18 at 19:51











  • Can you at least clarify where the problem exactly is? If there is some problem reading out a selected item in the browser, the problem is in no way related to PHP. If the markup is not generated properly, the problem is in no way related to JS

    – Nico Haase
    Nov 21 '18 at 19:51











  • Why are you doing document.getElementById ('availabilityChoice').innerHTML = val;? Why are you trying to replace the inner HTML of the select element with its value?

    – Alon Eitan
    Nov 21 '18 at 19:57














-1












-1








-1


0






I searched this website and couldn't find an answer.
I have a php page that display a dropdown list with 2 choices. When on change, a Javascript execute a function that opens a php page to update my database, sending the ID and another parameter with the URL. The problem is the ID value is always the same (the last of the list). Can you help me please, I am very new in PHP and Javascript. Thank you in advance. Here is the PHP code and the javascript:






	<?php
$AvailabilityCondition = "1=1";

if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}


$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";



if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;

?>






<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >

<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>

</form>

<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>



<!-- **************************************************************************************************************** -->
</td>
<td>
<a href="rental-delete.php?id=<?php echo ($rowrentals["id"])?>" onclick="return confirm('Are you sure? This CAN NOT be undone!')">!!! DELETE !!!</a>
</td>
</tr>
</table>
</div>


<?php
}}



/* free result set */
$rentalresult->free();

/* close connection */
$link->close();
?>

<br/><br/>
<div align="center">
<a href="index.php"><b>Back to Managers Main Menu</b></a>
</div>

</body> ​





I just posted more of my PHP page. So you can see the query and the WHILE, where $rowrentals["id"] is coming from.



Here is also a screen capture of how the page looks like: screen capture



I echoed the $rowrentals["id"] under each availability dropdown.
But whatever row I chose to change the availability, it always pass Id=9, the last one. That is what confuses me.



Before the screen capture, all five rows where "Available". Then I selected "Rented" on the first row. The page updated and since Id always =9, you can see the last row "Rented".



The Javascript is working to retrieve the value of the selected item. Because it opens this page perfectly: status-update.php?rented=Yes&Id=9
But again, Id is always 9...










share|improve this question
















I searched this website and couldn't find an answer.
I have a php page that display a dropdown list with 2 choices. When on change, a Javascript execute a function that opens a php page to update my database, sending the ID and another parameter with the URL. The problem is the ID value is always the same (the last of the list). Can you help me please, I am very new in PHP and Javascript. Thank you in advance. Here is the PHP code and the javascript:






	<?php
$AvailabilityCondition = "1=1";

if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}


$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";



if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;

?>






<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >

<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>

</form>

<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>



<!-- **************************************************************************************************************** -->
</td>
<td>
<a href="rental-delete.php?id=<?php echo ($rowrentals["id"])?>" onclick="return confirm('Are you sure? This CAN NOT be undone!')">!!! DELETE !!!</a>
</td>
</tr>
</table>
</div>


<?php
}}



/* free result set */
$rentalresult->free();

/* close connection */
$link->close();
?>

<br/><br/>
<div align="center">
<a href="index.php"><b>Back to Managers Main Menu</b></a>
</div>

</body> ​





I just posted more of my PHP page. So you can see the query and the WHILE, where $rowrentals["id"] is coming from.



Here is also a screen capture of how the page looks like: screen capture



I echoed the $rowrentals["id"] under each availability dropdown.
But whatever row I chose to change the availability, it always pass Id=9, the last one. That is what confuses me.



Before the screen capture, all five rows where "Available". Then I selected "Rented" on the first row. The page updated and since Id always =9, you can see the last row "Rented".



The Javascript is working to retrieve the value of the selected item. Because it opens this page perfectly: status-update.php?rented=Yes&Id=9
But again, Id is always 9...






	<?php
$AvailabilityCondition = "1=1";

if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}


$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";



if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;

?>






<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >

<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>

</form>

<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>



<!-- **************************************************************************************************************** -->
</td>
<td>
<a href="rental-delete.php?id=<?php echo ($rowrentals["id"])?>" onclick="return confirm('Are you sure? This CAN NOT be undone!')">!!! DELETE !!!</a>
</td>
</tr>
</table>
</div>


<?php
}}



/* free result set */
$rentalresult->free();

/* close connection */
$link->close();
?>

<br/><br/>
<div align="center">
<a href="index.php"><b>Back to Managers Main Menu</b></a>
</div>

</body> ​





	<?php
$AvailabilityCondition = "1=1";

if ((isset($_POST["availabilityChoice"])) && ($_POST["availabilityChoice"] !== "All")){
$AvailabilityCondition = "rented = "."'".$_POST["availabilityChoice"]."'";
}


$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";



if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;

?>






<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability" method=POST" class="form-horizontal" >

<select onChange="showSelected(this.value)" id ="availabilityChoice" name="availabilityChoice" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select> <?php echo ($rowrentals["id"]) ?>

</form>

<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script>



<!-- **************************************************************************************************************** -->
</td>
<td>
<a href="rental-delete.php?id=<?php echo ($rowrentals["id"])?>" onclick="return confirm('Are you sure? This CAN NOT be undone!')">!!! DELETE !!!</a>
</td>
</tr>
</table>
</div>


<?php
}}



/* free result set */
$rentalresult->free();

/* close connection */
$link->close();
?>

<br/><br/>
<div align="center">
<a href="index.php"><b>Back to Managers Main Menu</b></a>
</div>

</body> ​






javascript php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 1:28







Joshua Stokes

















asked Nov 21 '18 at 19:33









Joshua StokesJoshua Stokes

12




12













  • If i understand your question correctly you need to check the selected value not the value see this example var optionSelected = $("option:selected", this); VS var valueSelected = this.value;`

    – Puya Sarmidani
    Nov 21 '18 at 19:42













  • Possible duplicate of Retrieving the text of the selected <option> in <select> element

    – basic
    Nov 21 '18 at 19:48











  • As Puya described you need to get the value of the selected option. See this SO article: stackoverflow.com/questions/1085801/…

    – J. Schmale
    Nov 21 '18 at 19:51











  • Can you at least clarify where the problem exactly is? If there is some problem reading out a selected item in the browser, the problem is in no way related to PHP. If the markup is not generated properly, the problem is in no way related to JS

    – Nico Haase
    Nov 21 '18 at 19:51











  • Why are you doing document.getElementById ('availabilityChoice').innerHTML = val;? Why are you trying to replace the inner HTML of the select element with its value?

    – Alon Eitan
    Nov 21 '18 at 19:57



















  • If i understand your question correctly you need to check the selected value not the value see this example var optionSelected = $("option:selected", this); VS var valueSelected = this.value;`

    – Puya Sarmidani
    Nov 21 '18 at 19:42













  • Possible duplicate of Retrieving the text of the selected <option> in <select> element

    – basic
    Nov 21 '18 at 19:48











  • As Puya described you need to get the value of the selected option. See this SO article: stackoverflow.com/questions/1085801/…

    – J. Schmale
    Nov 21 '18 at 19:51











  • Can you at least clarify where the problem exactly is? If there is some problem reading out a selected item in the browser, the problem is in no way related to PHP. If the markup is not generated properly, the problem is in no way related to JS

    – Nico Haase
    Nov 21 '18 at 19:51











  • Why are you doing document.getElementById ('availabilityChoice').innerHTML = val;? Why are you trying to replace the inner HTML of the select element with its value?

    – Alon Eitan
    Nov 21 '18 at 19:57

















If i understand your question correctly you need to check the selected value not the value see this example var optionSelected = $("option:selected", this); VS var valueSelected = this.value;`

– Puya Sarmidani
Nov 21 '18 at 19:42







If i understand your question correctly you need to check the selected value not the value see this example var optionSelected = $("option:selected", this); VS var valueSelected = this.value;`

– Puya Sarmidani
Nov 21 '18 at 19:42















Possible duplicate of Retrieving the text of the selected <option> in <select> element

– basic
Nov 21 '18 at 19:48





Possible duplicate of Retrieving the text of the selected <option> in <select> element

– basic
Nov 21 '18 at 19:48













As Puya described you need to get the value of the selected option. See this SO article: stackoverflow.com/questions/1085801/…

– J. Schmale
Nov 21 '18 at 19:51





As Puya described you need to get the value of the selected option. See this SO article: stackoverflow.com/questions/1085801/…

– J. Schmale
Nov 21 '18 at 19:51













Can you at least clarify where the problem exactly is? If there is some problem reading out a selected item in the browser, the problem is in no way related to PHP. If the markup is not generated properly, the problem is in no way related to JS

– Nico Haase
Nov 21 '18 at 19:51





Can you at least clarify where the problem exactly is? If there is some problem reading out a selected item in the browser, the problem is in no way related to PHP. If the markup is not generated properly, the problem is in no way related to JS

– Nico Haase
Nov 21 '18 at 19:51













Why are you doing document.getElementById ('availabilityChoice').innerHTML = val;? Why are you trying to replace the inner HTML of the select element with its value?

– Alon Eitan
Nov 21 '18 at 19:57





Why are you doing document.getElementById ('availabilityChoice').innerHTML = val;? Why are you trying to replace the inner HTML of the select element with its value?

– Alon Eitan
Nov 21 '18 at 19:57












2 Answers
2






active

oldest

votes


















0














Try with this:



<script type="text/javascript">
function showSelected(val){
document.getElementById ('availabilityChoice').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals['id']) ?>");
}




If showSelected javascript function inside a foreach/while loop you need to extract it from loop and send id+value at the same time to showSelected function.






share|improve this answer
























  • I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

    – Joshua Stokes
    Nov 22 '18 at 2:33



















0














Thank you everybody. I found my answer. I added a counter, so this create a different function name for each database Id.



Here is the code:






$rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
$counter =0;
if ($rentalresult = $link->query($rentalquery)) {
/* fetch associative array */
while ($rowrentals = $rentalresult->fetch_assoc()) {
$NumberOfResults = $rentalresult->num_rows;
$counter = $counter+1;
?>


<!-- ************************* This display a table with a short rental description (Region name - Rental name)
****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
<div align="center">
<table >
<tr >
<td width="300" >
<?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
</td>
<td width="50">
<a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
</td>
<td width="100" align="right">

<!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
<form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

<select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

<option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

<option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
</select>

</form>

<script type="text/javascript">
function showSelected<?php echo $counter ;?>(val){
document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
}
</script> ​








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419341%2fphp-and-javascript-issue-function-return-always-the-last-value%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try with this:



    <script type="text/javascript">
    function showSelected(val){
    document.getElementById ('availabilityChoice').innerHTML = val;
    window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals['id']) ?>");
    }




    If showSelected javascript function inside a foreach/while loop you need to extract it from loop and send id+value at the same time to showSelected function.






    share|improve this answer
























    • I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

      – Joshua Stokes
      Nov 22 '18 at 2:33
















    0














    Try with this:



    <script type="text/javascript">
    function showSelected(val){
    document.getElementById ('availabilityChoice').innerHTML = val;
    window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals['id']) ?>");
    }




    If showSelected javascript function inside a foreach/while loop you need to extract it from loop and send id+value at the same time to showSelected function.






    share|improve this answer
























    • I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

      – Joshua Stokes
      Nov 22 '18 at 2:33














    0












    0








    0







    Try with this:



    <script type="text/javascript">
    function showSelected(val){
    document.getElementById ('availabilityChoice').innerHTML = val;
    window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals['id']) ?>");
    }




    If showSelected javascript function inside a foreach/while loop you need to extract it from loop and send id+value at the same time to showSelected function.






    share|improve this answer













    Try with this:



    <script type="text/javascript">
    function showSelected(val){
    document.getElementById ('availabilityChoice').innerHTML = val;
    window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals['id']) ?>");
    }




    If showSelected javascript function inside a foreach/while loop you need to extract it from loop and send id+value at the same time to showSelected function.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 19:53









    Erdem MedreErdem Medre

    261




    261













    • I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

      – Joshua Stokes
      Nov 22 '18 at 2:33



















    • I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

      – Joshua Stokes
      Nov 22 '18 at 2:33

















    I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

    – Joshua Stokes
    Nov 22 '18 at 2:33





    I tried to replace the double quotes with single quotes like you suggested. But it doesn't change a thing. Like you said my problem is to extract the value from the loop and I do not know how.

    – Joshua Stokes
    Nov 22 '18 at 2:33













    0














    Thank you everybody. I found my answer. I added a counter, so this create a different function name for each database Id.



    Here is the code:






    $rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
    $counter =0;
    if ($rentalresult = $link->query($rentalquery)) {
    /* fetch associative array */
    while ($rowrentals = $rentalresult->fetch_assoc()) {
    $NumberOfResults = $rentalresult->num_rows;
    $counter = $counter+1;
    ?>


    <!-- ************************* This display a table with a short rental description (Region name - Rental name)
    ****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
    <div align="center">
    <table >
    <tr >
    <td width="300" >
    <?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
    </td>
    <td width="50">
    <a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
    </td>
    <td width="100" align="right">

    <!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
    <form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

    <select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

    <option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

    <option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
    </select>

    </form>

    <script type="text/javascript">
    function showSelected<?php echo $counter ;?>(val){
    document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
    window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
    }
    </script> ​








    share|improve this answer




























      0














      Thank you everybody. I found my answer. I added a counter, so this create a different function name for each database Id.



      Here is the code:






      $rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
      $counter =0;
      if ($rentalresult = $link->query($rentalquery)) {
      /* fetch associative array */
      while ($rowrentals = $rentalresult->fetch_assoc()) {
      $NumberOfResults = $rentalresult->num_rows;
      $counter = $counter+1;
      ?>


      <!-- ************************* This display a table with a short rental description (Region name - Rental name)
      ****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
      <div align="center">
      <table >
      <tr >
      <td width="300" >
      <?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
      </td>
      <td width="50">
      <a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
      </td>
      <td width="100" align="right">

      <!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
      <form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

      <select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

      <option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

      <option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
      </select>

      </form>

      <script type="text/javascript">
      function showSelected<?php echo $counter ;?>(val){
      document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
      window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
      }
      </script> ​








      share|improve this answer


























        0












        0








        0







        Thank you everybody. I found my answer. I added a counter, so this create a different function name for each database Id.



        Here is the code:






        $rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
        $counter =0;
        if ($rentalresult = $link->query($rentalquery)) {
        /* fetch associative array */
        while ($rowrentals = $rentalresult->fetch_assoc()) {
        $NumberOfResults = $rentalresult->num_rows;
        $counter = $counter+1;
        ?>


        <!-- ************************* This display a table with a short rental description (Region name - Rental name)
        ****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
        <div align="center">
        <table >
        <tr >
        <td width="300" >
        <?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
        </td>
        <td width="50">
        <a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
        </td>
        <td width="100" align="right">

        <!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
        <form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

        <select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

        <option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

        <option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
        </select>

        </form>

        <script type="text/javascript">
        function showSelected<?php echo $counter ;?>(val){
        document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
        window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
        }
        </script> ​








        share|improve this answer













        Thank you everybody. I found my answer. I added a counter, so this create a different function name for each database Id.



        Here is the code:






        $rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
        $counter =0;
        if ($rentalresult = $link->query($rentalquery)) {
        /* fetch associative array */
        while ($rowrentals = $rentalresult->fetch_assoc()) {
        $NumberOfResults = $rentalresult->num_rows;
        $counter = $counter+1;
        ?>


        <!-- ************************* This display a table with a short rental description (Region name - Rental name)
        ****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
        <div align="center">
        <table >
        <tr >
        <td width="300" >
        <?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
        </td>
        <td width="50">
        <a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
        </td>
        <td width="100" align="right">

        <!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
        <form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

        <select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

        <option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

        <option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
        </select>

        </form>

        <script type="text/javascript">
        function showSelected<?php echo $counter ;?>(val){
        document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
        window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
        }
        </script> ​








        $rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
        $counter =0;
        if ($rentalresult = $link->query($rentalquery)) {
        /* fetch associative array */
        while ($rowrentals = $rentalresult->fetch_assoc()) {
        $NumberOfResults = $rentalresult->num_rows;
        $counter = $counter+1;
        ?>


        <!-- ************************* This display a table with a short rental description (Region name - Rental name)
        ****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
        <div align="center">
        <table >
        <tr >
        <td width="300" >
        <?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
        </td>
        <td width="50">
        <a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
        </td>
        <td width="100" align="right">

        <!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
        <form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

        <select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

        <option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

        <option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
        </select>

        </form>

        <script type="text/javascript">
        function showSelected<?php echo $counter ;?>(val){
        document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
        window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
        }
        </script> ​





        $rentalquery = "SELECT * FROM rentals WHERE $AvailabilityCondition ORDER BY region_name, rental_name";
        $counter =0;
        if ($rentalresult = $link->query($rentalquery)) {
        /* fetch associative array */
        while ($rowrentals = $rentalresult->fetch_assoc()) {
        $NumberOfResults = $rentalresult->num_rows;
        $counter = $counter+1;
        ?>


        <!-- ************************* This display a table with a short rental description (Region name - Rental name)
        ****************************** with and EDIT link, a DELETE link and an AVAILABILITY selector. ******************* -->
        <div align="center">
        <table >
        <tr >
        <td width="300" >
        <?php echo ($rowrentals["region_name"]).' - '.($rowrentals["rental_name"]).'<br/>'; ?>
        </td>
        <td width="50">
        <a href="rental-mod.php?id=<?php echo ($rowrentals["id"]) ?>">EDIT</a>
        </td>
        <td width="100" align="right">

        <!-- *********** This form display the actual availability state. On change, it updates the database **************** -->
        <form name="updateAvailability<?php echo $counter ?>" method=POST" class="form-horizontal" >

        <select onChange="showSelected<?php echo $counter ;?>(this.value)" id ="availabilityChoice<?php echo $counter ?>" name="availabilityChoice<?php echo $counter ?>" style="width: auto">

        <option value="No" <?php if (($rowrentals["rented"]) == "No") echo 'selected="selected"' ?>>Available</option>

        <option value="Yes" <?php if (($rowrentals["rented"]) == "Yes") echo 'selected="selected"' ?>>Rented</option>
        </select>

        </form>

        <script type="text/javascript">
        function showSelected<?php echo $counter ;?>(val){
        document.getElementById ('availabilityChoice<?php echo $counter ;?>').innerHTML = val;
        window.location.replace("status-update.php?rented=" + val + "&id=<?php echo ($rowrentals["id"]) ?>");
        }
        </script> ​






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:52









        Joshua StokesJoshua Stokes

        12




        12






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419341%2fphp-and-javascript-issue-function-return-always-the-last-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?