PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 13, 2022

[FIXED] Store value from input to database

 January 13, 2022     html, php, phpmyadmin     No comments   

Issue

This is the input type I currently have in index.php:

<fieldset>
      <input type="text" placeholder="GSM" name="gsm" required>
</fieldset>

In the saveToDb.php I have this:

$gsm = $_POST['gsm'];

It all works fine, but I want to change from text input to drop down. I have managed to load values into my drop down like this:

<?php
    $filename = 'values.txt';
    $eachlines = file($filename, FILE_IGNORE_NEW_LINES);//create an array
    echo '<select name="value" id="value">';
    foreach($eachlines as $lines){
        echo "<option>{$lines}</option>";
        
    }
    echo '</select>';
?>

How can I store the values from dropdown to database, insted of of input text?


Solution

You can assign a value on each option:

echo '<select name="name-of-the-field" id="value">';
foreach($eachlines as $lines){
    echo "<option value=\"{$lines}\">{$lines}</option>";
}

and the value you will receive is the one of the option selected (in order to get the value, nothing changes, you still have to use $selected_value = $_POST['name-of-the-field'];)



Answered By - Alberto Sinigaglia
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing