If input type text is not empty

Hello,

if(!empty($_POST['multiswitch_id']) && !empty($_POST['multiswitch_adet']) ){
  $teklifarray['multiswitch']         = array_combine($_POST['multiswitch_id'], $_POST['multiswitch_adet']);
}

<form>
<input name="multiswitch_id[]" type="text" />
<input name="multiswitch_adet[]" type="text" />
</form>

I couldn’t get the result I wanted because it was a arrays

What I want is just add it as an array to the variable $teklifarray['multiswitch']

Thank you

The whole purpose of using an array in a form is to make it easier to extract the data once it is submitted.

<form id="formData" class="checkStyle" action="create.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="cms[user_id]" value="3">
    <input type="hidden" name="cms[author]" value="<?= Login::full_name() ?>">
    <input type="hidden" name="action" value="upload">
    <div class="file-style">
        <input id="file" class="file-input-style" type="file" name="image">
        <label for="file">Select file</label>
    </div>
    <select class="select-css" name="cms[page]">
        <option value="index">Home</option>
        <option value="blog" selected>Blog</option>
        <option value="about">About</option>
    </select>
    <div class="heading-style">
        <label class="heading_label_style" for="heading">Heading</label>
        <input class="enter_input_style" id="heading" type="text" name="cms[heading]" value="" tabindex="1" required
               autofocus>
    </div>
    <div class="content-style">
        <label class="text_label_style" for="content">Content</label>
        <textarea class="text_input_style" id="content" name="cms[content]" tabindex="2"></textarea>
    </div>
    <div class="submit-button">
        <button class="form-button" type="submit" name="submit" value="enter">submit</button>
    </div>
</form>

When the form is submitted then do this

if (($_SERVER[‘REQUEST_METHOD’] === ‘POST’) && isset($_POST[‘submit’], $_FILES[‘image’])) {
$data = $_POST[‘cms’]; // Extract the array
// continue with process the data

I could be wrong as I really don’t know what you are trying to do, but use arrays to your advantage and it will make the other coding easier in my opinion.

Thank you for the answer
There are multiple inputs in the form
After the form is submitted, I convert all values to json and write them to the database.
I do this to avoid creating multiple tables in the database
If an input is empty, it should not be registered in the database.

can something like this happen? Is it correct?

if(!empty($_POST['multiswitch_id']) && !empty($_POST['multiswitch_adet']) ){
  $teklifarray['multiswitch'] = array($_POST['multiswitch_id']=>$_POST['multiswitch_adet']);
}

<form>
<input name="multiswitch_id" type="text" />
<input name="multiswitch_adet" type="text" />
</form>

Well, what Strider64 was saying is that when values are posted to a form, null fields (not empty ones) are not even posted, therefore there is no need to check for empty ones. To explain a little further, a input field can be several things data-wise.

NOT-SET ( Means the field was never accessed and does not exist. )
NULL ( Means the field is 100% empty and never had anything in it. )
EMPTY ( Means the field is set but has nothing inside it. )
DATA ( Means the field actively contains data. )

If you post a form using the if (($_SERVER[‘REQUEST_METHOD’] === ‘POST’) check, it means that the user submitted the form. This means that there might be live data or not. ALL live data, if present, would be inside the $_POST[ ] array. You could save that if needed. But, it does not tell you if individual fields were entered or not. There fore you need to check for these things:

If each value ISSET because if a field started empty and the user did not change it, then it would never exist in the $_POST[ ] array. You check it with " if isset($_POST[‘some-field-name’]) ".

If a field is set, meaning it does exist, you need to check it to see if it is empty. To do that you check it with " if !empty($_POST[‘some-field-name’]) ".

Therefore you need to use both of these for testing each field. But, back to your question…

If you want to combine your posted multiswitch’s id and adet, are both of these inside the $_POST array? You could do it the same way you last posted. But, one problem with this, you combine “id” and “adet” into a combined array, but, this does not create a valid index for it. How would you know what the “multiswitch” array index be? That index would not tell you want is stored in the combined array. It would make more sense to save the combined array as the index. But, yes, you can combine posted data into an array as you posted. I would suggest that in your database, you store it as three entries. The “multiswitch” as the index and two fields for the two arrays of data, id’s and adet’s. Makes your life easier. Just deal with the data, not attempting to combine the into one field.

One more thing I forgot to mention. A NULL field value is NOT EMPTY ! If you load the fields from a database and a field is set to NULL, the form field will also be NULL. Which means is it set and it has a value. Therefore it it not empty. You can test for that using ==NULL or ===NULL depending on your needs. In your case if you want to combine arrays, you might need to alter them to remove the null values. You could do that in the post array, but, complicated. The way around this would be not to store NULL’s in your fields when loaded from the database. Hope this does not confuse you…

This is incorrect. In a properly coded form, save for checkboxes, EVERY field will be isset. Therefore, isset is pointless and redundant.

Well, you have not seen his extremely complicated code. It does a ton of JS code to change fields and has hundreds and hundreds of them. Read a lot of his other posts…
And, his question was how to check data which might not be there because of his JS AJax calls.
SO, perhaps you are correct, but, not sure…

1 Like

@ErnieAlex She has a lot of knowledge about the project I did, so she says it right. :grinning: :grinning: :+1: :+1:

There is a long list as in the picture below.
When either “ID” or “Quantity” is empty on a product, this product should not be included in the array
Since I use “[]” square brackets in the names of the inputs, “empty” is invalid.
Why did I use square brackets? I used for “array_combine”
The purpose here is, when we want to remove a product from this list, when we delete one or both of the “ID” or “Quantity” section of the relevant product and submit a form, this product will no longer be in the series.

You may ask, why are you doing such a thing to remove the product from the list?
The products selected with the wizard are created as a array, this array is saved in the database as json in a single table.
This is not an option that will be used much, but I do this option in case there is a need to change the product quantity or remove the product.
NOTE:This option is for administrators

OR If you need to explain briefly
Edit and save the data saved as json in the database in the edit field.
or remove some products and save

Firstly, as @benanamen has stated, only un-checked check-box/radio-button fields won’t be set, and as an array field name, the only way you would get an empty array would be if all the check-box/radio-buttons making up that field are un-checked. For all other field types, they will be set, regardless of what value they contain, and as an array field name, they will never be an empty array.

The user interface for this activity should include displaying the part number/part description and display the current quantity as the only form field for each item. The ID is an internal value that the user of this page should not need to know, see, or care about. You would use the ID value as the field’s array index, so that the submitted data will already be a single array with the ids as the indexes and the quantities as the element values.

Once you do that, you can remove entries in the submitted data that have either been cleared (which will be an empty string) or set to zero (a string consisting of a ‘0’ character), by using array_filter() without using any call-back function.

Sponsor our Newsletter | Privacy Policy | Terms of Service