Jump to content

Recommended Posts

I am new to the board, and there are a lot of posts here so I am apologizing now if this question has been asked/answered a million times, but I do not even know how to begin a search for the answer. Here is what I have...

 

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

<input type=text name=text1>

<input type=button name=button1 onclick="submit()">

<input type=button name=button2 onclick="submit()">

</form>

 

My question is... in the XXX.php document, is there a way I can tell, which button was clicked?

 

This question may be more HTML or JavaScript related than PHP, but I was uncertain where to post it.

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/
Share on other sites

Alright. Thanks. Maybe my button names are something crazy that I didn't intend. I am using a count variable to name them. So... to check for my error, how could I get the name of the button clicked to print on the screen within onclick()?

 

<input type=button name="(unknown)" onclick="document.write(...)">

 

What would replace the ... to display the name of the button name, which is defined, but hypothetically unknown?

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375251
Share on other sites

there is no such thing as a dumb question.

every question counts towards greater knowledge.

except for those real dumb questions..lol

 

but yeah that is DHMTL that you are using.

 

to do it with php you would have to add a value attribute to your button

so lets say that you have 3 buttons

 

<input type=button name="unknown"  value='1' onclick="document.write(...)">

<input type=button name="unknown" value='2' onclick="document.write(...)">

<input type=button name="unknown" value='3' onclick="document.write(...)">

 

and to check it it would be

 

if(isset($_POST['unknown'])){

print $_POST['unknown'];// PRINTS OUT THE NUMBER
}

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375257
Share on other sites

unknown wasn't actually the button name. I was saying hypothetically if we didn't know the button name.

 

Alright. This is frustrating me. I will just post the actual code because it is not working as I want.

 

editSpaces.php

if ($_POST['delete'])
{
echo "Record Deleted";
}
else
{
echo "Record Saved";
}

 

manageSpaces.php

while($row = mysql_fetch_array($spaces))
  {
  echo '<form action="editSpaces.php" method="POST">';
  echo "<input type=text disabled=1 name=address value='" . $row['address'] . "' /> ";
  echo "<input type=text disabled=1 name=details value='" . $row['details'] . "' /> ";
  echo "<input type=text disabled=1 name=cost value='" . $row['cost'] . "' /> ";
  echo "<input type=text disabled=1 name=contact value='" . $row['contact'] . "' /> ";
  echo "<input type=button name='save_" . $count . "' value='Save' onClick='submit()' disabled=1> ";
  echo "<input type=button name='delete' value='Delete' onClick='submit()'>";
  echo "</form>";
  
  $count++;
  }

 

There are some inconsistencies (naming of two buttons) in the code from me just guessing and checking to try to solve this. $count is defined at 0 before the while loop. The save button's disabled is set to 0 when another action occurs. Any clue where I'm going wrong?

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375258
Share on other sites

here is an example

 

<?php
if(isset($_POST['delete'])){

// delete from table where record_id=$_POST['record_id']

}elseif(isset($_POST['save'])){

// update table set record_id=$_POST['record_id']

}

while($row = mysql_fetch_array($spaces))
  {
  echo '<form action="editSpaces.php" method="POST">';
  echo "<input type=text disabled=1 name=address value='" . $row['address'] . "' /> ";
  echo "<input type=text disabled=1 name=details value='" . $row['details'] . "' /> ";
  echo "<input type=text disabled=1 name=cost value='" . $row['cost'] . "' /> ";
  echo "<input type=text disabled=1 name=contact value='" . $row['contact'] . "' /> ";
  echo "<input type=button name='save_" . $count . "' value='Save' onClick='submit()' disabled=1> ";
  echo "<input type=button name='delete' value='Delete' onClick='submit()'>";

//add the hidden field to store the record number

echo "<input name=\"record_id\" type=\"hidden\" id=\"record_id\" value=\"{$row['record_id']}\" />
  ";
  echo "</form>";
  
  $count++;
  }
  ?>

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375263
Share on other sites

manageSpaces.php

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
<script type="text/javascript">
function disable(form) {
form.address.disabled = 0;
form.details.disabled = 0;
form.cost.disabled = 0;
form.contact.disabled = 0;
}
</script>

</head>

<body>

<form action="addSpaces.php" method="post">
Address: <input type="text" name="address" />
Details: <input type="text" name="details" />
Cost: <input type="text" name="cost" />
Contact: <input type="text" name="contact" />
<input type="submit" />
</form>

<?php
$con = mysql_connect(...);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db(...);

$spaces = mysql_query("SELECT * FROM spaces");
$count = 0;

while($row = mysql_fetch_array($spaces))
  {
  echo '<form action="editSpaces.php" method="POST">';
  echo "<input type=text disabled=1 name=address value='" . $row['address'] . "' /> ";
  echo "<input type=text disabled=1 name=details value='" . $row['details'] . "' /> ";
  echo "<input type=text disabled=1 name=cost value='" . $row['cost'] . "' /> ";
  echo "<input type=text disabled=1 name=contact value='" . $row['contact'] . "' /> ";
  echo "<input type=button name='save_" . $count . "' value='Save' onClick='if (confirm(\"Are you sure you want to save the changes?\")){disable(this.form);submit()}' disabled=1> ";
  echo "<input type=button name='edit_" . $count . "' value='Edit' onClick='disable(this.form);this.form.address.disabled=1;this.disabled=1; save_" . $count . ".disabled=0'> ";
  echo "<input type=button name='delete' value='Delete' onClick='if (confirm(\"Are you sure you want to delete this space?\")){disable(this.form);submit()}'>";
  echo "</form>";
  
  $count++;
  }

mysql_close($con);
?>

</body>

</html>

 

editSpaces.php

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Filename</title>
<script type="text/javascript">
</script>
</head>

<body>

<?php
$con = mysql_connect(...);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
mysql_select_db(...);


if (isset($_POST['delete']))
{
//	mysql_query("DELETE FROM spaces WHERE address='" . $_POST[address] . "'");
echo "True";
}
else
{
//	mysql_query("UPDATE spaces SET details='" . $_POST[details] . "' cost='" . $_POST[cost] . "' contact='" . $_POST[contact] . "' WHERE address='" . $_POST[address] . "'");
}
?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375268
Share on other sites

  echo "<input type=button name='save_" . $count . "' value='Save' onClick='submit()' disabled=1> ";
  echo "<input type=button name='delete' value='Delete' onClick='submit()'>";

 

I just wanted to point out that the reason $_POST['delete'] is not set is because you set the type attribute to button.  If you want the button name to appear in $_POST the input type has to be submit

  <input type="submit" name="a_button" value="Go" />

 

Why in the heck are you using Javascript to submit the button anyways?  You do realize if someone comes along with JS disabled they won't be able to use your form, right?

 

I'm going to watch a movie, I'll check this again in the morning.

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375270
Share on other sites

echo "<input type=submit name='delete' value='Delete' onClick='if (confirm(\"Are you sure you want to delete this space?\")){disable(this.form);submit()}'>";

 

That won't work though with the type as a submit. Even if the user hits cancel, it will still submit. I'm teaching myself PHP / MySQL, so I am not really sure all I have available yet.

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375279
Share on other sites

Where am I screwing up on the updating?

 

mysql_query("UPDATE spaces SET details='" . $_POST['details'] . "' cost='" . $_POST['cost'] . "' contact='" . $_POST['contact'] . "' WHERE address='" . $_POST[address] . "'");

 

mysql_query("UPDATE spaces SET details='" . $_POST['details'] . "' AND cost='" . $_POST['cost'] . "' AND contact='" . $_POST['contact'] . "' WHERE address='" . $_POST[address] . "'");

 

Neither would work.

Link to comment
https://forums.phpfreaks.com/topic/74269-solved-is-it-possible/#findComment-375291
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.