Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-21-2012, 10:23 AM   PM User | #1
terle
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
terle is an unknown quantity at this point
Submit to same page, and read value

Hi

I wanna create a todo-list for myself and girlfriend, so when ever I remember that we need something, I can write it down. And then whom ever of us in the supermarket can then buy it.
And I'm new to html... and I'm starting from scratch

My problem is:
How do I read a value from a textbox and write it???

I have a form looking like this:
Code:
<form action="editList.php" method="post">
        <?php writeCheckBoxes(); ?>
	<label>Indtast ny vare</label>
	<input type="checkbox" name="itemNames[]" value="test" />
	<div class="six columns">
		<input type="text" name="tekstFelt" placeholder="Vare navn" />
	</div>
	<div class="six columns">
		<a href="#" class="tiny round success button">Tilføj</a>
		<input type="submit"/>
	</div>
</form>
And the php function
PHP Code:
function writeCheckBoxes()
{
$counter = length($_POST[]);
while ($a <$counter)
{
?><input type="checkbox" name="itemNames[]" value="<?= $itemNames[$counter]; ?>" />
<?= $itemNames[$counter]; ?>
<br /><?
}
}
So how do I get the value from tekstFelt and use the function writeCheckBoxes???

Thank you
terle is offline   Reply With Quote
Old 07-21-2012, 12:58 PM   PM User | #2
Will Bontrager
Regular Coder

 
Join Date: Jun 2012
Location: Near Chicago, USA
Posts: 123
Thanks: 7
Thanked 19 Times in 19 Posts
Will Bontrager is an unknown quantity at this point
Quote:
Originally Posted by terle View Post
Hi

I wanna create a todo-list for myself and girlfriend, so when ever I remember that we need something, I can write it down. And then whom ever of us in the supermarket can then buy it.
And I'm new to html... and I'm starting from scratch

My problem is:
How do I read a value from a textbox and write it???

I have a form looking like this:
Code:
<form action="editList.php" method="post">
        <?php writeCheckBoxes(); ?>
	<label>Indtast ny vare</label>
	<input type="checkbox" name="itemNames[]" value="test" />
	<div class="six columns">
		<input type="text" name="tekstFelt" placeholder="Vare navn" />
	</div>
	<div class="six columns">
		<a href="#" class="tiny round success button">Tilføj</a>
		<input type="submit"/>
	</div>
</form>
And the php function
PHP Code:
function writeCheckBoxes()
{
$counter = length($_POST[]);
while ($a <$counter)
{
?><input type="checkbox" name="itemNames[]" value="<?= $itemNames[$counter]; ?>" />
<?= $itemNames[$counter]; ?>
<br /><?
}
}
So how do I get the value from tekstFelt and use the function writeCheckBoxes???

Thank you

Checkbox values won't submit to the next page unless the checkbox is checked before the form is submitted.

The below publishes the item with a checkbox (for printing and taking to the supermarket). And it also publishes a hidden field to hold the value for use when the form is submitted again.

I kept your style, mostly.

Code:
<form action="editList.php" method="post">

<?php
function writeCheckboxItem($value)
{
   $value = stripslashes($value); // To remove the "\" of any escaped quote characters.
   $value = htmlspecialchars($value,ENT_QUOTES); // Convert quotes, angle brackets, and ampersand for attribute value.
   echo "<label>$value</label> <input type='checkbox'/><br/>\n";
   echo "<input type='hidden' name='itemNames[]' value='$value'/>";
}

if( ! empty($_POST['itemNames']) )
{
   foreach( $_POST['itemNames'] as $item )
   {
      writeCheckboxItem($item);
   }
}
if( ! empty($_POST['tekstFelt']) )
{
   writeCheckboxItem($_POST['tekstFelt']);
}
?>

<div class="six columns">
	<input type="text" name="tekstFelt" placeholder="Vare navn" />
</div>
<div class="six columns">
	<a href="#" class="tiny round success button">Tilføj</a>
	<input type="submit"/>
</div>
</form>
Will
__________________
Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]
Will Bontrager is offline   Reply With Quote
Users who have thanked Will Bontrager for this post:
terle (07-21-2012)
Old 07-21-2012, 01:58 PM   PM User | #3
terle
New to the CF scene

 
Join Date: Jul 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
terle is an unknown quantity at this point
Thanks for answering

I realize now I wasn't very clear in my post as to what I really wanna do.
The goal is to build a simple shopping list.
My gf has an iPhone and myself an Android. On my server I want a website we both can access and see what we need.
On this site I want a list of items (with checkboxes) and a textfield for entering new items.
Somthing like this.

In relation to my question; how do take the new item and add it to the list with a checkbox?
terle is offline   Reply With Quote
Old 07-21-2012, 09:16 PM   PM User | #4
Hoatzin
New to the CF scene

 
Join Date: Nov 2011
Location: Denmark
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hoatzin is an unknown quantity at this point
Quote:
Originally Posted by terle View Post
Thanks for answering

I realize now I wasn't very clear in my post as to what I really wanna do.
The goal is to build a simple shopping list.
My gf has an iPhone and myself an Android. On my server I want a website we both can access and see what we need.
On this site I want a list of items (with checkboxes) and a textfield for entering new items.
Somthing like this.

In relation to my question; how do take the new item and add it to the list with a checkbox?
to style it for handheld devices remember the media="handheld" or something along that line in your html link to stylesheet

To get values from a form you will need to do either GET or POST the values.
Also the <input> tags MUST BE within the <form> tag aswell which i can see it isnt in the provided link.

Code:
<div class="row">

    <form action="editList.php#TabEditList" method="POST">
        <label>Input new Item</label>
        <div class="six columns">
            <input type="text" name="tekstFelt" placeholder="Item Name" />
        </div>
        <div class="six columns">
            <div class="twelve columns">
                <input type="checkbox" name="box" value="milk" />Milk<br />
                <input type="checkbox" name="box" value="bread" />Bread<br />
                <input type="checkbox" name="box" value="butter" />Butter<br />

                <input type="submit" href="#" class="tiny round success button"/>
            </div>
    </form>

</div>

</div>
next thing is to grab the stuff and save them into a database with PHP & SQL which is a scripting language (im only doing it with 2 checkboxes in this example for simplicity) We are grabbing the information send from the form with $_GET / $_POST there is a difference between those and if you wish i can explain the difference but else i wont.. but in this case its best to do it with POST.

And checkboxes are send as array s

Code:
<?php
//we have now started php and are ready to write the code
$boxes = $_POST["box"]; //get stuff

if($boxes) { //we are checking if the variable is useable if it is we will be able to run the below code if not nothing will happen.

    foreach($boxes as $box) {
        //First we will set all stuff to be undone
mysql_query("UPDATE `todo` SET `status` = 'undone'") or die(mysql_error());
       //and then we will set the checked boxes to be done with a loop going through the array we got.

        mysql_query("INSERT INTO `todo` (Id, stuff, status)
VALUES (NULL, '$box', 'done')") or die(mysql_error);
    }
}
?>
But with this approach you will need to have mysql database knowledge..
And you'll need to set up a table in the database called 'todo' with the rows id, stuff, status

And trust me things is not always as easy as they seem to be

There are also other ways of doing what you want, like writing to a file.. that would make it able to be moves across servers without having to worry about the database files..

hope this was helpfull
Hoatzin is offline   Reply With Quote
Old 07-22-2012, 01:51 PM   PM User | #5
Will Bontrager
Regular Coder

 
Join Date: Jun 2012
Location: Near Chicago, USA
Posts: 123
Thanks: 7
Thanked 19 Times in 19 Posts
Will Bontrager is an unknown quantity at this point
Quote:
Originally Posted by terle View Post
Thanks for answering

I realize now I wasn't very clear in my post as to what I really wanna do.
The goal is to build a simple shopping list.
My gf has an iPhone and myself an Android. On my server I want a website we both can access and see what we need.
On this site I want a list of items (with checkboxes) and a textfield for entering new items.
Somthing like this.

In relation to my question; how do take the new item and add it to the list with a checkbox?

[Reposting this as my previous submission seems to have been lost in cyberspace.]



Got it.

A database on the server will be needed. MySQL is good. A plain text database would work, too, in this instance.

A decision needs to be made what kind of database to use before code can be written for it.

The file viewed at the supermarket might work something like this (PHP is notes only, not real code):

Code:
<?php
if data was submitted with one or more checkboxes checked
{
	remove the checked items from the database
}
?>
<!doctype html>
<body>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<?php
for each item in the database
{
	print checkbox with value of item and print item next to it
}
?>
<input type="submit">
</form>
</body>
</html>
The person at the supermarket checks the items that are purchased and clicks the submit button. The database is updated.

The file viewed at home where items are added to the database might work something like this (PHP is notes only, not real code):

Code:
<?php
if an item was submitted 
{
	add item to the database
}
?>
<!doctype html>
<body>
<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
<?php
for each item in the database
{
	print checkbox and item next to it
}
?>
<input type="text" name="tekstFelt" placeholder="Vare navn">
<input type="submit">
</form>
</body>
</html>
Will
__________________
Numerology API for apps - Facebook, iPad, mobile phones. No charge to use API. [info]

Last edited by Will Bontrager; 07-22-2012 at 01:54 PM.. Reason: added info
Will Bontrager is offline   Reply With Quote
Reply

Bookmarks

Tags
html, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:24 AM.


Advertisement
Log in to turn off these ads.