PHP How to assign starting value to changing value? [duplicate]

PHP How to assign starting value to changing value? [duplicate]



This question already has an answer here:



I'm trying to make something that allows the user to change the "price" of an item. It's working so far, but my problem is that the prices show up as empty values on the first load of the page, giving me the "undefined index" error, which means that the user has to input prices first before it works as intended. I want to make it so that the prices have predefined values before loading.



Any help would be much appreciated.


<?php

$price1 = $_POST['price1'];
$price2 = $_POST['price2'];
$price3 = $_POST['price3'];
$price4 = $_POST['price4'];
$price5 = $_POST['price5'];
$price6 = $_POST['price6'];

?>

<h2>Admin</h2>

<form method="POST" action="admin.php">
<table>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Change Price</th>
</tr>
<tr>
<td>Double Cheesy Quarter Pounder</td>
<td><?php echo "PHP" . $price1;?></td>
<td><input type="text" name="price1">
</td>
</tr>
<tr>
<td>Crispy Bacon Burger</td>
<td><?php echo "PHP" . $price2;?></td>
<td><input type="text" name="price2"></td>
</tr>
<tr>
<td>Mushroom Burger</td>
<td><?php echo "PHP" . $price3;?></td>
<td><input type="text" name="price3"></td>
</tr>
<tr>
<td>Triple Bacon Burger</td>
<td><?php echo "PHP" . $price4;?></td>
<td><input type="text" name="price4"></td>
</tr>
<tr>
<td>Cheesy Bacon Fries</td>
<td><?php echo "PHP" . $price5;?></td>
<td><input type="text" name="price5"></td>
</tr>
<tr>
<td>Chocolate Frappe</td>
<td><?php echo "PHP" . $price6;?></td>
<td><input type="text" name="price6"></td>
</tr>
</table>
<input type="submit" value="Confirm">

</form>



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




4 Answers
4



on the first load of the page, giving me the "undefined index" error



You need to put check if the key exists:


$price1 = isset($_POST['price1']) ? $_POST['price1'] : '';



With PHP 7, its more simple:


$price1 = $_POST['price1'] ?? '';



You can set a default value if $_POST is not set:


$price1 = isset($_POST['price1']) ? $_POST['price1'] : 99;
$price2 = isset($_POST['price2']) ? $_POST['price2'] : 99;
$price3 = isset($_POST['price3']) ? $_POST['price3'] : 99;
$price4 = isset($_POST['price4']) ? $_POST['price4'] : 99;
$price5 = isset($_POST['price5']) ? $_POST['price5'] : 99;
$price6 = isset($_POST['price6']) ? $_POST['price6'] : 99;



From Php 7.0 you can use null coalescing:


<?php

$price1 = $_POST['price1'] ?? '';
// And so on.



This is the equivalent of:


<?php
$price1 = '';
if(isset($_POST['price1']))
$price1 = $_POST['price1'];



Or some other more verbose variation.



There are 2 things here, lets first take a look at your undefined index error



1)To remove undefined index error ,you first need to declare the php variabels as null at the top ,like


$price1=$price2=$price3=$price4=$price5=$price6='';



2)To have predefined values you need to include 'value' attribute to each of your input field like,


<input type="text" name="price3" value="<? php echo $price3; ?>" >

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)