Data from Form button

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1263 Views - Last Post: 17 November 2010 - 11:07 AM Rate Topic: -----

#1 phoenix112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 10

Data from Form button

Posted 16 November 2010 - 11:20 AM

G'day!

I have a table with 2 columns. The first column contains an Edit form button, the column next to it a name that corresponds to a database entry.

I have the variable 'cname1' as the name. I want to make it so when I hit the "Edit" button the name is passed to the php form. However, the value of the button is being passed and not its name... How can I fix this?

<input type='submit' name='cname1' value='Edit'/>


Is This A Good Question/Topic? 0
  • +

Replies To: Data from Form button

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 11:30 AM

by looking at the (array) key in the $_POST/$_GET array. thatís where the names are.
Was This Post Helpful? 0
  • +
  • -

#3 phoenix112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 10

Re: Data from Form button

Posted 16 November 2010 - 12:05 PM

I can get the names of text fields out fine. But with the button I am getting the value instead of the name.

This is how I am calling it:
$namequery = $_POST['cname1'];

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 12:13 PM

you always get the value. thatís what form elements do. a button is no exception there.
Was This Post Helpful? 0
  • +
  • -

#5 phoenix112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 10

Re: Data from Form button

Posted 16 November 2010 - 12:16 PM

Oh... Thanks. :oops:

So there is no way for a button to pass data that is not displayed on the button?
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 12:18 PM

why would you have a button pass data other than that?
Was This Post Helpful? 0
  • +
  • -

#7 phoenix112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 10

Re: Data from Form button

Posted 16 November 2010 - 12:23 PM

Well, I want a button to say Edit and contain a name. Its a table of names and each name has an edit button generated out of mysql. So, click the button by the name and be able to edit it. Perhaps I am looking in the wrong direction.
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 12:26 PM

then you would need a text field to contain the new name, right?

# maybe something like that?
<form action="file.php?nametoedit=oldname">
	<input type="text" name="newname">
	<button>Submit new Name</button>
</form>

This post has been edited by Dormilich: 16 November 2010 - 12:29 PM
Reason for edit:: added example

Was This Post Helpful? 0
  • +
  • -

#9 phoenix112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 10

Re: Data from Form button

Posted 16 November 2010 - 12:31 PM

There isn't really a new name, this is for existing entries. When the Edit button is pressed it calls a different script that puts the data into text forms. I have that part working fine, but I can't seem to pass the name to it.

Here is the full code for that page and a picture of its output.

<html>
<body>
<?php

$username = $userdata['user_name'];

$results = dbquery("SELECT * FROM crecords WHERE username = '$username'");


echo "<table><tr><td></td>";
echo "<td>Name:</td></tr>";

//Name: <input type="text" name="Fname" size="50"/>

if(dbrows($results)>0)
{
while($data=dbarray($results))
{
$cname1 = $data['name'];
echo "<tr><td><form action='http://localhost/cdb/viewpage.php?page_id=7' method='post'><input type='submit' name='cname1' value='Edit'/></td>";
echo "<td>",$cname1,"</td></tr>";
}}
echo "</form></table>";

?> 
</body>
</html>
Attached File  edit buttons'.bmp (59.46K)
Number of downloads: 50

This post has been edited by phoenix112: 16 November 2010 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 12:34 PM

echo '<tr><td><form action="http://localhost/cdb/viewpage.php?page_id=7&amp;cname1=' . $cname1 . '" method="post"><input type="submit" name="cname1" value="Edit"></form></td>';


PS. you’re wide open to SQL Injection

PPS. you current form won’t work as expected.

This post has been edited by Dormilich: 16 November 2010 - 01:20 PM
Reason for edit:: correcting code (missing closing tag)

Was This Post Helpful? 1
  • +
  • -

#11 phoenix112  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-November 10

Re: Data from Form button

Posted 16 November 2010 - 12:38 PM

Thank you! And I'm aware its unsecure. Working locally without a great knowledge of php. Going to get it working the way I want before I implement security and upload it.

Thanks again for sticking with it. :)
Was This Post Helpful? 0
  • +
  • -

#12 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: Data from Form button

Posted 16 November 2010 - 12:41 PM

Sounds to me like you need a hidden field with the name, if your page would look like

Name: Jimmy (EDIT BUTTON)

Name: James (EDIT BUTTON)


Since you want to get the name being passed, then for each name being brought in from the db, you need to have a hidden field inside the form with the value you want. Then POST to the receiving PHP page, and instead of grabbing the value of your button, grab the value of the hidden text field.
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 12:45 PM

View Postkinggeorge1987, on 16 November 2010 - 07:41 PM, said:

Sounds to me like you need a hidden field with the name,

hm, and how do you determine, which name to edit? eventually, all hidden fields will pass their value.

This post has been edited by Dormilich: 16 November 2010 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#14 kinggeorge1987  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 12
  • View blog
  • Posts: 130
  • Joined: 22-September 10

Re: Data from Form button

Posted 16 November 2010 - 03:38 PM

View PostDormilich, on 16 November 2010 - 11:45 AM, said:

View Postkinggeorge1987, on 16 November 2010 - 07:41 PM, said:

Sounds to me like you need a hidden field with the name,

hm, and how do you determine, which name to edit? eventually, all hidden fields will pass their value.


Well I thought he was speaking in terms of some sort of User Panel, like the user is logged in, and this is how he edits his information. So it wouldn't be a matter of how many names, it would just be a matter of who's logged in.
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: Data from Form button

Posted 16 November 2010 - 11:23 PM

apparently thatís not the case. the while() loop hints that there is more than one name.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2