Jump to content


Photo

Form and POST problem


  • Please log in to reply
5 replies to this topic

#1 bunker

bunker
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 April 2006 - 04:34 PM

Hi!

Just need some quick help please.. My script is very simple.. fichas.php has a form just to add a record to th Database with the data inserted in two fields.. but for some reason when the POST is used I dont get the values... The new record is created in the data base, but because I also pass a variable using _GET.

The code is

$ele_tec = $_POST['new_ele'];
$valor = $_POST['new_valor'];
$id_potencia = $_GET['id_potencia'];
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('$ele_tec', '$valor','$id_potencia')");

And the form;

echo "<form name=\"agregar\" action='" .$_SERVER['SELF'] ."' method='post'>";
echo "<br><input type=\"text\" name=\"new_ele\">";
echo "<input type=\"text\" name=\"new_valor\">";
echo "<input type=\"hidden\" name=\"id_potencia\" value=\"$row[id_potencia]\">";
echo "<input name=\"anadir\" type=\"submit\" value=\"Agregar\" onclick=\"location.href='fichas.php?action=agregar&id_potencia=$row[id_potencia]'\">";
echo "</form>";

There is some code, before, between and after.. But I think that the problem is in the code above.

Also.. how do I quote code in the forum??

Thx in advanced!!

#2 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 07 April 2006 - 04:37 PM

try doing it like this
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('{$ele_tec}', '{$valor}','{$id_potencia}')");


note the curly braces

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#3 bunker

bunker
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 April 2006 - 04:44 PM

[!--quoteo(post=362579:date=Apr 7 2006, 11:37 AM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ Apr 7 2006, 11:37 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
try doing it like this
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('{$ele_tec}', '{$valor}','{$id_potencia}')");
note the curly braces
[/quote]

Nop... Same result!

Ive decided to post all the code...

<?php include("../include/config.php");
$connection = mysql_connect("$dbhost", "$dblogin", "$dbpass");
$db = mysql_select_db("$dbname", $connection);
$sql = mysql_query("SELECT p.id AS pid, p.desc AS pdesc FROM potencia p");

$id = $_GET['id'];
$action = $_GET['action'];

switch ($action){
case "delete":
delete();
break;

case "agregar":
agregar();
break;
}

function delete() {
$id_elemento = $_GET['id_elemento'];
$sql = mysql_query("DELETE FROM elemento USING elemento WHERE elemento.id = '$id_elemento'");
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
header ("Location: fichas.php?id=1");
}

function agregar() {
$ele_tec = $_POST['new_ele'];
$valor = $_POST['new_valor'];
$id_potencia = $_GET['id_potencia'];
$sql = mysql_query("INSERT INTO elemento ( ele_tec, valor, id_potencia) values ('{$ele_tec}', '{$valor}','{$id_potencia}')");
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
echo $ele_tec;
//header ("Location: fichas.php?id=1");
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR...ml4/loose.dtd">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?php
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }
?>
<p>
<select name="potencia[]" id="potencia" onchange="location = this.options[this.selectedIndex].value;">
<option>- Seleccione -</option>
<?php
while ($row = mysql_fetch_array($sql)) {
echo "<option value=\"fichas.php?id=$row[pid]\">$row[pdesc]</option>";
}
?>

</select>
<?php
if (!empty($id)){

$sql = mysql_query("SELECT * FROM elemento WHERE id_potencia = $id");
if (mysql_error()) { print "Database ERROR: $sql " . mysql_error(); }

while ($row = mysql_fetch_array($sql)) {
echo "<br><input name=\"ele_tec\" type=\"text\" id=\"ele_tec\" value=\"$row[ele_tec]\">";
echo "<input name=\"valor\" type=\"text\" id=\"valor\" value=\"$row[valor]\">";
echo "<input name=\"editar\" type=\"button\" id=\"editar\" value=\"Editar\">";
echo "<input name=\"eliminar\" type=\"button\" id=\"eliminar\" value=\"Eliminar\" onclick=\"location.href='fichas.php?action=delete&id_elemento=$row[id]'\">";
}
$sql = mysql_query("SELECT id_potencia FROM elemento WHERE id_potencia = $id");
$row = mysql_fetch_array($sql);
echo "<form name=\"agregar\" action='" .$_SERVER['SELF'] ."' method='post'>";
echo "<br><input type=\"text\" name=\"new_ele\">";
echo "<input type=\"text\" name=\"new_valor\">";
echo "<input type=\"hidden\" name=\"id_potencia\" value=\"$row[id_potencia]\">";
echo "<input name=\"anadir\" type=\"submit\" value=\"Agregar\" onclick=\"location.href='fichas.php?action=agregar&id_potencia=$row[id_potencia]'\">";
echo "</form>";
}
?>
</p>

</body>
</html>


Thx once again!


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 07 April 2006 - 05:08 PM

At the start of the script place the following debugging code:
<?php
if (isset($_POST['anadir'])) {
    echo '<pre> ---- $_GET ---- ' . print_r($_GET,true) . '</pre>';
    echo '<pre> ---- $_POST ---- ' . print_r($_POST,true) . '</pre>';
}?>
to see what is being passed back to your script.

In your function, put the query into a separate string and echo it to see what the query looks like.

Ken


#5 bunker

bunker
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 07 April 2006 - 09:42 PM

[!--quoteo(post=362588:date=Apr 7 2006, 12:08 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 7 2006, 12:08 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
At the start of the script place the following debugging code:
<?php
if (isset($_POST['anadir'])) {
    echo '<pre> ---- $_GET ---- ' . print_r($_GET,true) . '</pre>';
    echo '<pre> ---- $_POST ---- ' . print_r($_POST,true) . '</pre>';
}?>
to see what is being passed back to your script.

In your function, put the query into a separate string and echo it to see what the query looks like.

Ken
[/quote]

Done.. And the result is a NOTHING... When echoing the Sql query I just get a simple 1 ... because on the GET variable.

Could it be something to do with the form? Ive checked it several times but I see nothing wrong!

Bye

#6 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 08 April 2006 - 11:37 AM

You cannot do this:
echo "<input name=\"anadir\" type=\"submit\" value=\"Agregar\" onclick=\"location.href='fichas.php?action=agregar&id_potencia=$row[id_potencia]'\">";
As the submit button will over ride the javascript (location.href) command when submitting your form.

If you want to pass GET data then you'll need to place that in the action attribute in the form tag. IE:
echo "<form action=" . $_SERVER['PHP_SELF'] . "?action=agregar&id_potencia=" . $row[id_potencia] . ">";
But you cannot use the location.href command when submitting the form as the form will override this.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users