user123_456
user123_456

Reputation: 5805

<form> tag is not shown when created with php

I have this code:

$html .='<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" id="input_clone_id" name="input_clone_id" value="'.$row['id'].'"/>
    <input type="hidden" id="input_clone_var" name="input_clone_var" value="V"/>
    <input type="file" name="uploaded_files[]" id="input_clone" multiple="multiple" /><br />
    <input type="submit" style="margin-left:0;float:left" value="Upload Files" />
</form>';
$html .='<a href="#" onclick="$.ajax({type: \'POST\',url: \'delete_pic.php\',data:{id:\''.$row['id_vila'].'\',var:\'V\',val:\''.$val.'\'},cache: false});window.location.reload( true );" style="background:url(\'images/icons/delete.png\') 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';

everything is created correctly except the <form> tag. It is not shown. What am I doing wrong in here?

Thanks

EDIT:

function formatImage2($col,$val,$row)
    {
        $html = '';
        $new = explode( ';', $val );

        for($j = 0; $j < count($new)-1; $j++) 
        {
            $html .= '<a target="_blank" href="../images/gallery/'.$new[$j].'"><img style="border: none;" src="../images/gallery/'.$new[$j].'" alt="'.$new[$j].'" width="100" /></a>';
        }
        $html .='<form enctype="multipart/form-data" action="upload.php" method="POST">
                        <input type="hidden" id="input_clone_id" name="input_clone_id" value="'.$row['id'].'"/>
                        <input type="hidden" id="input_clone_var" name="input_clone_var" value="V"/>
                        <input type="file" name="uploaded_files[]" id="input_clone" multiple="multiple" /><br />
                        <input type="submit" style="margin-left:0;float:left" value="Upload Files" />
                </form>';
        $html .='<a href="#" onclick="$.ajax({type: \'POST\',url: \'delete_pic.php\',data:{id:\''.$row['id_vila'].'\',var:\'V\',val:\''.$val.'\'},cache: false});window.location.reload( true );" style="background:url(\'images/icons/delete.png\') 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a>';

        return $html;
    }

I'm using this to format pictures from the database.

OUTPUT:

<td>
                        <input type="hidden" id="input_clone_id" name="input_clone_id" value="9">
                        <input type="hidden" id="input_clone_var" name="input_clone_var" value="V">
                        <input type="file" name="uploaded_files[]" id="input_clone" multiple="multiple"><br>
                        <input type="submit" style="margin-left:0;float:left" value="Upload Files">

                <a href="#" onclick="$.ajax({type: 'POST',url: 'delete_pic.php',data:{id:'9',var:'V',val:''},cache: false});window.location.reload( true );" style="background:url('images/icons/delete.png') 50% -19px no-repeat;width:16px;height:16px;float:left;margin-left:10px;margin-top: 6px;"></a></td>

Upvotes: 1

Views: 1514

Answers (2)

David
David

Reputation: 1093

This is caused by having one form nested in another. The inner form tag will not be rendered, but any html inside that form will be.

You must have some form wrapping this one in your outside HTML.

<form id="outsideForm">
    <form id="insideForm">    
        <div>Inside Content</div>
    </form>
</form>

Inside form element disappears - Demo

I posted this info 3 hours ago, but it was downvoted/deleted because it was in the form of a question. I would have posted the info as a comment, but I didn't have the reputation to do so. A better solution would probably have been to edit my answer, instead of removing it completely.

Upvotes: 3

Code Lღver
Code Lღver

Reputation: 15603

Your code is good. Whenever you write the HTML code and try to print in PHP then always use the PHP's pre-build function htmlentities().

Like:

echo htmlentities($html);

This function is used to identify the HTML code in PHP string and to encode that. Try this. This will work for you.

Before using it you need to decode your string in HTML entities. Use such as:

$html .= html_entity_decode('<form enctype="multipart/form-data" action="upload.php" method="POST">');

This is an example. you need to do with all string to escape with special characters. Here is the tutorial of this.

EDITED:

 $html .= <<<HTML

    <form enctype="multipart/form-data" action="upload.php" method="POST">
                        <input type="hidden" id="input_clone_id" name="input_clone_id" value="'.$row['id'].'"/>
                        <input type="hidden" id="input_clone_var" name="input_clone_var" value="V"/>
                        <input type="file" name="uploaded_files[]" id="input_clone" multiple="multiple" /><br />
                        <input type="submit" style="margin-left:0;float:left" value="Upload Files" />
                </form>

HTML;

Try this.

Upvotes: 2

Related Questions