Jump to content

Recommended Posts

I have a button that increases a counter (which also a link to a file) beside it.

echo "<tr>";
echo "<td name='".$cell1['value']."'>".$cell1['value']."</td><td name='".$cell2['value']."'><a href='file.php'>".$counter."</a><input type='button' name='buttonName' onclick='submit('.$cell1['value'].');'></td>
<td name='".$cell2['value']."'><a href='file.php'>".$counter."</a><input type='button' name='buttonName' onclick='submit('.$cell1['value'].');'></td>";
echo "</tr>";

When one of the buttons is clicked I want to pass cell1value to the processing script.  Is this the correct way to do it?  And how do I access $cell1['value'] after submission? 

Link to comment
https://forums.phpfreaks.com/topic/236202-passing-php-value-with-submit-button/
Share on other sites

No, what MadTechie's suggestion does is passes the value of $cell1['value'] in your url.

 

So if $cell1['value'] is set to 'hello' you'll see the following code

<a href='file.php?mycell=<?php echo $cell['value'];?>'>

Will produce the following url yousite.com/file.php?mycell=hello

 

When you echoi $_GET['mycell'] in file.php it will return 'hello'. It doesn't access the variable $cell1['value'].

Sorry I didn't explain properly what I am trying to do.

I am writing a form table to a page with php.  This consists of an entry number (auto_increment), a reference number, text, and 3 buttons for different types of users.  Its akin to a voting system of sorts you might say.  If a user agrees with something, they click a certain button.  This increases a counter beside each button.  Avoiding double counting, once a user clicks a button, only one submission counts.  When the user clicks their button, I check which button was clicked using isset(), that determines the user type, I then check if the user has clicked previously, if yes, do nothing, if no, write the users name to a text file.  As I mentioned beside each button is a link to the text file showing the number of users name's in the file like this

<a href='Filename.txt'>5</a>, showing 5 names in the file.  Clicking on the link will open the text file with the names.  I have a table for each user type into which I enter the line number (auto_increment) and the users name.  I then query the table, write to the txt file and update the counter on the page.

So I need to submit the line number and user type.  The button name is the usertype and I want to pass the line number via the onclick function.  Hope that wasn't to big a mouthful!

I don't quite understand..

however i have written a script to choose to vote yes,no, decide later

Now for portability reasons it will ask who you are first, (John,Sam or Peter) these are the users, and instead of using a database i am using a session,

So no database is involved on this example

How close am i ? (probably way off)

 

<?php
$UsersDB = array(1=>'John',2=>'Sam',3=>'Peter');
$votes = array(1=>'Yes',2=>'No',3=>'Later');
session_start();
if(!empty($_GET['rest_user'])) $_SESSION['member_id'] = 0;

//setup votes table
if(!isset($_SESSION['votes'])||isset($_GET['rest_votes'])) $_SESSION['votes'] = array();

//Select user
if(!empty($_POST['Members'])){
  $uID = $_POST['Members'];
}else{
  //members ID
  $uID = !empty($_SESSION['member_id'])?$_SESSION['member_id']:0;
}
$_SESSION['member_id'] = $uID;

//Make choice (exclude Later or no selection)
if(!empty($_POST['vote']) && $_POST['vote'] != 3){
  $choice = $votes[$_POST['vote']];
  $_SESSION['votes'][$uID] = $choice;
}
$_SESSION['member_id'] = $uID;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>voteing</title>
</head>
<body>
<!-- clear url-->
<form action="?" method="post">
<?php
if($uID==0){
  //List members
  echo "<p>who are you ?</p>";
  foreach($UsersDB as $id=>$name) echo '<label><input type="radio" name="Members" value="'.$id.'" id="Members" />'.$name.'</label><br />'.PHP_EOL;
}else{
  //member selected
  echo "Hello ".$UsersDB[$uID]."<br />\n";

  //OKAY
  if(!isset($_SESSION['votes'][$uID])){
    echo "<p>Please vote</p>";
    foreach($votes as $id=>$choice) echo '<label><input type="radio" name="vote" value="'.$id.'" id="vote" />'.$choice.'</label><br />'.PHP_EOL;
  }else{
    echo "You selected ".$_SESSION['votes'][$uID]."<br />\n";
  }
}
if(!empty($_GET['view_votes'])){
  //would be done froma query
echo "<BR />-----------------<BR />\n";
  $y=0;$n=0;
  foreach($_SESSION['votes'] as $userid => $vote){
    echo $UsersDB[$userid]." voted $vote<br/>\n";
    if($vote=='Yes') $y++;
    if($vote=='No') $n++;

  }
  echo "<BR /><strong>Total</strong><BR />\n";
  echo "YES: $y<BR />\n";
  echo "NO: $n<BR />\n";
  echo "-----------------<BR />\n";
}
?>
  <input name="User" type="submit" value="submit" /><br />
  <a href="?rest_user=1">select another user</a><br />
  <a href="?rest_votes=1">clear votes</a><br />
  <a href="?view_votes=1">show votes</a><br />
</form>
</body>
</html>

Each user type has a seperate counter and button.  Initially all counters are zero. 

<tr><td>LineNo</td><td>Reference</td><td>Text</td><td>0</td><td>button1</td><td>0</td><td>Button2</td><td>0</td><td>Button3</td></tr>

When a user (logged in) clicks the appropriate button for the first time, their Username and LineNo are inserted into a table for their user type.  The table is queried, counting the users grouped by the Line no which becomes the counter for the User Type, the username is then written to a txt file, the counter becoming a link to the txt file.  The result being like this:

<tr><td>LineNo</td><td>Reference</td><td>Text</td><td><a href='filename.txt'>1</a></td><td>button1</td>
<td><a href='filename.txt'>2</a></td><td>Button2</td><td><a href='filename.txt'>9</a></td><td>Button3</td></tr>

As the counters are all initially 0, I need to create a link for the txt file including counter and insert it into the cell to replace the 0.  I assume javascript is required to insert link into cell.  Subsequent clicks by any user have no affect, no double-counting.

When one of the buttons is clicked, how do I submit the LineNo on the row a button is clicked? 

echo "<tr><td name='LineNo[]>LineNo</td><td>Reference</td><td>Text</td><td><a href='filename.txt'>1</a></td><td>button1</td>
<td><a href='filename.txt'>2</a></td><td>Button2</td><td><a href='filename.txt'>9</a></td><td>Button3</td></tr>";

If I use onclick='submit('$LineNo['value']');' how do I access the LineNo using $_POST?

$_POST['LineNo'] ?

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.