0

I have a list of organizations in a DDL where the Text displays the full name of each organization and the Value is the key for that organization. I need both the full name and the key entered into my database when an organization is selected. I have been unable to access both the Value and the Text using PHP, so I ended up stringing the full name and key together in the Value field and using Explode to split them into different variables (see below). Is this the best way to do this? I want to make sure I'm not overlooking something cleaner and simpler.

HTML:

<form method="post" action="submit.php" name="requestDetailsForm">

<label>Organization Name:</label>
<select id="org_name" name="org_name">
    <option value="Organization 1.abc">Organization 1</option>
    <option value="Organization 2.def">Organization 2</option>
</select>
<input type="Submit" value="Submit">
</form>

PHP:

<?php
$org_name = mysql_escape_string($_POST['org_name']);

$org_name_split = explode('.', $org_name);
$org_name_full = $org_name_split[0];
$org_name_key = $org_name_split[1];

$strSQL = "INSERT INTO Database(org_name_full, org_name_key) VALUES ('$org_name_full', '$org_name_key')"; 
?>

3 Answers 3

1

I would use jQuery to do this one, I would add a hidden element and assign the value as the option's text value on form submission.

<form method="post" action="submit.php" name="requestDetailsForm">

    <label>Organization Name:</label>
    <select id="org_name" name="org_name">
        <option value="Organization 1.abc">Organization 1</option>
        <option value="Organization 2.def">Organization 2</option>
    </select>
    <input type='hidden' name='option_text'value='' >
    <input type="Submit" value="Submit">
</form>

<script type="text/javascript">
    $('form').on('submit', function(e){
        e.preventDefault();
        var text = $('#org_name').find(":selected").text();
        $('input[name="option_text"]').val(text);
        $('form').submit();
    });
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Without PHP @joe42 is correct in saying JavaScript is going to be your best solution. Alternatively, you can camel case the option values or something and then use PHP to reformat it correctly. If you are happy just to use the labels, then you can exclude the value field from the syntax and the value will be equal to the option label.
0

There is no way to access both the selected value and the name through something like the $_POST array.

However, what I would generally do in a situation like this is create a table in the database of all possible organizations with their names and codes. In PHP, I would generate the list of select options using SELECT * on that database when generating the HTML for the original page. Then, when the data is submitted you just insert the organization code into the second table that you're dealing with. When you need to know the full organization code and name associated with the record in that table, you use a JOIN statement.

For example, say the original database that you're dealing with now represents users and each user has an organization. When someone submits the form, you insert a new user record into that table and insert the organization code into an organization_id field in that same users table. Then, when you want to know the organization code and name for a given user, you do SELECT * FROM users LEFT JOIN organizations ON user.organization_id = organization.id WHERE user.id = blah. This will return the user record along with their organization code and name.

1 Comment

Thanks for the advice. This is a new tool I'm building and as I think through the different use cases, I think you're right. Creating the additional table is probably the best way to store the data.
0

This is the best way when only using PHP.

A possible improvement could be to improve how you split the POST in variables:

list($org_name_full, $org_name_key) = explode('.', $org_name);

Otherwise you should use javascript to add either variable you need to the post

Comments

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.