...

View Full Version : Can an input type have multiple names and values



Foster
11-18-2011, 11:09 AM
As the title says 'Can an input type have multiple names and values?'

I have a radio button the needs to submit 2 different names and values to be processed by a php file and the info entered in to a database.


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

<input type="hidden" name="_submit_check" value="1"/>

<table width="98%" border="1" cellpadding="3" cellspacing="1" bgcolor="#000000">
<tr>
<td bgcolor="#000000"><font>&nbsp;</td>
<td bgcolor="#000000"><font><nobr>Weapon Name</nobr></font></td>
<td bgcolor=""><font>Description</font></td>
<td bgcolor=""><font>Price</font></td>
<tr><td bgcolor="" valign="top"><font size="1" face="Verdana"><input type="radio" name="Weapons" value="Brass Knuckles, "><input type="hidden" name="Money" value="125"></td><td bgcolor="" valign="top"><font size="1" face="Verdana">Brass Knuckles</td><td bgcolor="" width="90%"><font size="1" face="Verdana"><strong>DMG</strong>: +5<BR><strong>Special</strong>: None<BR><strong>Restrictions:</strong>: None<BR>Want to pack a bit more in your punch? This finely crafted brass piece fits over the knuckles with a brace fitting into your palm. Perfect for gaining the upper hand in a fist fight.</td><td bgcolor="" valign="top"><font size="1" face="Verdana">$125</font>

the section in blue is the bit i need submitted together, there are several of these on each page and at the moment when it submits it uses the last hidden value on the page not the one next to the specific item.

Kor
11-18-2011, 12:00 PM
Not very clear. Do you want that, if a radio button is checked (let's say your with the name="Weapons" and value="Brass Knuckles"), the query should send another pair of name/value (let's say your name="Money" value="125")?

Or both (if the radio is checked)?

Foster
11-18-2011, 12:08 PM
i need it to send both, so if weapons is checked it sends both weapons/brass knuckles and money/125

Kor
11-18-2011, 12:29 PM
The simpler way could be to send multiple data units, separated with a delimiter, as a single value:


<input type="radio" name="Weapons" value="Brass Knuckles_Money_125">

Now, at the server-side level all you have to do is to split (upon the delimiter) the value and use the sub strings accordingly. If PHP, I might show you the code.

Foster
11-18-2011, 12:30 PM
ok, how would i do that? would seeing the php side help?

Kor
11-18-2011, 12:46 PM
Could be something based on this:


<?php
if(isset($_GET['Weapons'])){
$data=explode('_',$_GET['Weapons']);

$name='Weapons';
$value=$data[0];
$secondname=$data[1];
$secondvalue=$data[2];
}
?>

if you method is GET. If it is POST, the global is $_POST

Foster
11-18-2011, 12:52 PM
is the the php file used.


<?php
include("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

mysql_query("UPDATE userProperties SET Weapons = CONCAT(Weapons, '".$_POST['Weapons']."')
WHERE name = '".$_POST['name']."' AND password = '".$_POST['password']."'");

mysql_query("update userStats set Money = Money - '".$_POST['Money']."' where name = '".$_POST['name']."'");
$res= mysql_query($update);

?>

where in there would i put the code you suggest. sorry i'm new to php

Kor
11-18-2011, 12:59 PM
Try


<?php
include("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

if(isset($_POST['Weapons'])){
$data=explode('_',$_POST['Weapons']);

$name='Weapons';
$value=$data[0];
$secondname=$data[1];
$secondvalue=$data[2];


mysql_query("UPDATE userProperties SET Weapons = CONCAT(Weapons, '".$value."')
WHERE name = '".$_POST['name']."' AND password = '".$_POST['password']."'");

mysql_query("update userStats set Money = Money - '".$secondvalue."' where name = '".$_POST['name']."'");
$res= mysql_query($update);
}
?>

Kor
11-18-2011, 01:04 PM
In fact, you need only two values, so that:


<input type="radio" name="Weapons" value="Brass Knuckles_125">

and:


<?php
include("config.php");

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

if(isset($_POST['Weapons'])){
$data=explode('_',$_POST['Weapons']);
$value=$data[0];
$secondvalue=$data[1];


mysql_query("UPDATE userProperties SET Weapons = CONCAT(Weapons, '".$value."')
WHERE name = '".$_POST['name']."' AND password = '".$_POST['password']."'");

mysql_query("update userStats set Money = Money - '".$secondvalue."' where name = '".$_POST['name']."'");
$res= mysql_query($update);
}
?>

Foster
11-18-2011, 01:12 PM
you my friend are a hero. i've been stuck on this for days, thank you

Foster
02-08-2012, 04:27 PM
would it be possible to do this for a 3rd and 4th value? the people who i built the site for have asked for additional data to be entered to the database with the same query



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum