<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<form action="2.php" method="post">
<table width="400" border="0">
    <tr>
        <td>Ders sayisi :</td>
        <td><input type="text" name="sayi" /></td>
    </tr>
    <tr>
        <td><input type="submit" name="gonder" value="Gonder" /></td>

    </tr>
</table>
</form>
<?php
    if(isset($_POST['gonder']))
    {

        for($i=1;$i<=$_POST['sayi'];$i++)
        {
            echo "$i.ders"." "."<input type='text' **name='e$i'** />"."   -   "." 
            <select name='dersler'><option **value='a1'**>A1</option>
             <option **value='a2'**>A2</option><option **value='b1'**>B1</option>
             <option **value='b2'**>B2</option><option **value='c'**>C</option></select>"."<br>";
        }
    }
    //......
?>
</body>

Bold Text Here
How can I use html elements in echo command with php ? for example I want to calculate the average of lessons in these ones by using name tags.

Dani AI

Generated

Quick summary: the original approach works but needs two corrections to make the generated fields useful for later processing. First, make sure the dynamically created inputs and the submit button are actually inside a single HTML form so their values are posted. Second, give repeated inputs array-style names so PHP receives them as arrays (for example name="scores[]" rather than distinct scalar names). ’s note about quote-escaping is valid — to avoid messy escapes either close PHP and emit plain HTML or use a heredoc. ’s point about array names and proper form placement is the key to computing an average.

A simple server-side processing pattern (assumes fields were posted as scores[]):

<?php
if (!empty($_POST['scores'])) {
    $trimmed = array_map('trim', $_POST['scores']);
    $numeric = array_filter($trimmed, 'is_numeric');
    $values = array_map('floatval', $numeric);

    if (count($values) > 0) {
        $average = array_sum($values) / count($values);
        echo 'Average: ' . round($average, 2);
    } else {
        echo 'No valid numeric values submitted.';
    }
}
?>

Practical notes: validate the initial lesson count before generating inputs; include a hidden field or different submit name to distinguish the “generate” step from the “calculate” step; handle empty and non-numeric entries (skip or report errors) to avoid division by zero; escape any echoed labels with htmlspecialchars to prevent XSS. For a smoother UX, add client-side JavaScript to add/remove fields and then submit the names[] / scores[] arrays to PHP for the averaging step.

Recommended Answers

All 4 Replies

You can write out HTML tags in PHP as you would normally. The only thing you need to be aware of is by using speech marks you either need to escape them or use quotation marks instead. What I'm trying to say is you can't use the same set twice as you'll end up ending your echo before you meant to. I hope I explained that well?

Example of how not to do it:

echo("<div id = "DivisionTag" > Contents </div>");

Example of how to do it:

echo("<div id = 'DivisionTag' > Contents </div>");

Member Avatar for Member #120589

You're questions doesn't make sense. Average of what?

name='e$i'

Is fine, what's the problem?

However,

<select name='dersler'>

needs to be:

<select name='dersler[]'>

But the whole page makes very little sense. You're creating select dropdowns OUTSIDE the form, or do not have form tags around the dropdowns. You have no submit button to go with them. I can't see any javascript, so I'm assuming this is a mistake?

I just tried to do that when I enter a number in textbox it gives to some textboxes and then how can I use created these textboxes for another processing ? Thank you for answers.

Member Avatar for Member #120589

I just tried to do that when I enter a number in textbox it gives to some textboxes and then how can I use created these textboxes for another processing ? Thank you for answers.

Sorry, that doesn't make any sense to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.