0

hi I have a html form with a select, this select the options do not have the actual value to avoid sql injections. form html

<select type="text" name="code">
                <option value="1">domain3.net</option>
                <option value="2">domain2.com</option>
                <option value="3">domain.es</option>
            </select> 

I am getting correctly all the variables, only that I wish that now this value of the select is transformed into the actual value is a domain tld, which is located in a static array. my php

  $dor = $_POST['code'];
  $dom = array(
    1 => 'domain3.net', 
    2 => 'domain2.com', 
    3 => 'domain.es'
    );
$domain = (in_array($dor, $dom));
echo $domain;

I hope you can help me

2
  • 3
    The value in $dor will be the key for your array: $domain = $dom[$dor]; Commented Jan 19, 2015 at 11:43
  • 2
    I think this will be enough if(isset($dom[$dor])) echo $dom[$dor]; Commented Jan 19, 2015 at 11:43

4 Answers 4

3

I can understand the reasoning behind your approach but this would be a nightmare to maintain. Every time you support a new top level domain you'll have to edit code in more than likely multiple places.

Use a simple database table to store the available domain types and then use the contents to populate your forms. It will be a lot more robust for validation and extensibility.

Sign up to request clarification or add additional context in comments.

2 Comments

we only have 5 domains just want to give chance to these 3 not going to be someone to inject code of a domain is not desired.
That's fair enough, if you are 100 percent sure then the comments and answers added before would work. In my experience if something can be improved to make it a bit more robust and developer friendly then it should be. The table could be used later to add more domains or even have relationships for customers or dns records. It isn't much work and I would argue that it would be less work in the long run. Either way, I know too little about your project to recommend the best course of action for your situation so I'll wish you the best of luck with your project.
2

Use array_key_exists function to match key into your static array.

 $dor = $_POST['code'];;
    $dom = array(
    1 => 'domain3.net', 
    2 => 'domain2.com', 
    3 => 'domain.es'
    );
$domain = (array_key_exists($dor, $dom))?$dom[$dor]:'';
echo $domain;

Comments

1

I'm a beginner at PHP too.. But I think what you need is :

$domain = $dom[$dor]; 

in_array is used to check whether the value exists or not.
To fetch the value itself, you will have to access the array element with an index or key.

Comments

0
<?php
$dor = $_REQUEST['code'];
  $dom = array(
    1 => 'domain3.net', 
    2 => 'domain2.com', 
    3 => 'domain.es'
    );
foreach($dom as $key=>$value)
{

 if($key==$dor)
 {
   echo "Domain is : ".$value; 

    break; 
 }



}

?>

1 Comment

Code-only answers offer little help if they're not accompanied by an explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.