Jump to content

Recommended Posts

okay, I have index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Functions!</title>
</head>
<body>
<p>Please enter your first and last name so I can 
give you your initials.</p>

<form action="functions.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" /></p>
<p>Last Name: <input type="text" name="last_name" size="20" /></p>
<input type="submit" name="submit" value="Get My Initials" />
</form>
</body>
</html>

and function.php

<?php

function first_and_last($first_name, $last_name ) {
    return $first_name[0] . '. ' . $last_name[0] . '.';
}
?>

if i'm posting $first_name and $last_name from the index.php page to the functions.php page where would I put the $_POST? or how would i write it into the code

Link to comment
https://forums.phpfreaks.com/topic/253007-function/#findComment-1297153
Share on other sites

i have this, but for some reason that i cant figure out it's giving me extra letters.  if i enter kelly norton, it outputs fknl or jerry lewis, fjll, here's the code:

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Functions!</title>
</head>
<body>
<p>Please enter your first and last name so I can 
give you your initials.</p>

<form action="functions.php" method="post">


<?php
for ($i = 0; $i < 5; $i++)
{
    // Maybe do a fieldset to separate each group.
    print '<legend>Enter Name</legend>';
    print('<label for="first_name%d">First Name:</label>', $i);
    print('<input id="first_name%d" type="text" name="name[][first]" size="20" />', $i);
    print('<label for="last_name%d">Last Name:</label>', $i);
    print('<input id="last_name%d" type="text" name="name[][last]" size="20" />', $i);
    //print '</fieldset>';
}
?>
<input type="submit" name="submit" value="Get My Initials" />


</body>
</html>

functions.php

<?php


if (isset($_POST['name']) && is_array($_POST['name']))
{
    foreach ($_POST['name'] AS $fullname)
    {
        list($first, $last) = each($fullname);
        print first_and_last($first, $last);
    }
}
function first_and_last($first_name, $last_name ) {
    return $first_name[0] . '. ' . $last_name[0] . '.';

}
//echo first_and_last($_POST['first_name'], $_POST['last_name']);
?>

Link to comment
https://forums.phpfreaks.com/topic/253007-function/#findComment-1297204
Share on other sites

Your code has 'notice'

Therefore you must correct it, this way:

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Functions!</title>
</head>
<body>
<p>Please enter your first and last name so I can 
give you your initials.</p>

<form action="functions.php" method="post">
<?php
for ($i = 0; $i < 5; $i++)
{
    // Maybe do a fieldset to separate each group.
    print '<legend>Enter Name</legend>';
    printf('<label for="first_name%d">First Name:</label>', $i);
    printf('<input id="first_name%d" type="text" name="name[][first]" size="20" />', $i);
    printf('<label for="last_name%d">Last Name:</label>', $i);
    printf('<input id="last_name%d" type="text" name="name[][last]" size="20" />', $i);
    //print '</fieldset>';
}
?>
<input type="submit" name="submit" value="Get My Initials" />
</body>
</html>

functions.php

<?php
if (isset($_POST['name']) && is_array($_POST['name']))
{
    foreach ($_POST['name'] AS $fullname)
    {
        list($first, $last) = each($fullname);
        print first_and_last($first, $last);
    }
    echo '<br/>';
}
function first_and_last($first_name, $last_name ) {
    return (!empty($first_name) && !empty($last_name)) ? $first_name[0] . '. ' . $last_name[0] . '.' : '';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/253007-function/#findComment-1297222
Share on other sites

OK. This is residual code. And extra letters is missing :)

fnction.php

<?php
if (isset($_POST['name']) && is_array($_POST['name']))
{
    $array = $_POST['name'];
    for($i = 0; $i < sizeof($array); $i++)
    {
        $first = !empty($array[$i]['first']) ? $array[$i]['first'] : '';
        $last = !empty($array[$i + 1]['last']) ? $array[$i + 1]['last'] : '';
        echo first_and_last($first, $last);
    }
}
function first_and_last($first_name, $last_name ) {
    return (!empty($first_name) && !empty($last_name)) ? $first_name[0] . '. ' . $last_name[0] . '.' : '';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/253007-function/#findComment-1297241
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.