0

I have been working on my own PHP project. I have hit an obstacle. I am trying to retrieve the results of a database and print out a form for each result set.

I then wish to interact with one particular result either be deleting it or passing it into a function etc..

Heres is my current code :

    <?php
                while($row = mysqli_fetch_array($result)){
                echo '<div id="post">'; ?>
                <form action="" method="post">
            <?php
                echo "<font size=4>".$row['post']."<br>"; 
                echo "posted by : ".$row['username']."<br>";
                $id = $row['p_id']; ?>
                <input type="submit" name="choice" value="Y">
                <input type="submit" name="choice" value="N">
                </form>
                <br>
                </div>
            }
    <?php
    if($_POST['choice']=="Y"){
     // progress
     functionA();
    }
    else if($_POST['choice']=="N"){
     // delete or remove
     functionB();
    }
 ?>

So my goal here would be click Y to progress that particular result or N to delete/remove the result. However currently by clicking either button all results either get deleted or progress. I do know that the id should be used to differentiate between posts but I cant quite seem to get it to work. Once the button is pressed it passes all results to either function.

2
  • 1
    I see that you are ending your PHP tag ?> then you have PHP code but without an opening PHP tag : <?php is this your actual code? Commented Mar 20, 2016 at 1:11
  • @CodeGodie no sorry this was just a snippet of the code. I have editied the above to have better structure Commented Mar 20, 2016 at 1:17

2 Answers 2

1

First of all, I suppose you want to know the record to delete. So, add an input to your form:

<input type="hidden" name="id" value="<?php echo $row['p_id']; ?>">

Then, in your script, call:

if( $_POST['choice'] == "Y" )
{
    // progress
    functionA( $_POST['id'] );
}
elseif( $_POST['choice']=="N" )
{
    // delete or remove
    functionB( $_POST['id'] );
}

Additional problem: how you can use your db connection inside the functions? Assuming your mysqli connection is named $conn, call the function(s) in this way:

functionA( $_POST['id'], $conn );

Side note: First process $_POST values, then retrieve db records and print it.

Side note 2: take a look at prepared statement.


Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I had dumbed my code down in an attempt to make it work. thank you for the help
0

Use two different <input type="radio">s for each item with the id in the value and let the user select which to delete and which to process.

So for an item with id 1 the outputted HTML would look like:

<input type="radio" name="items[1]" value="delete"><input type="radio" name="items[1]" value="process">

The name forces the items to go to _POST as an array with the ids as keys and the selected action as their values.

Example html: https://jsfiddle.net/gu1gkwod/

2 Comments

thank you very much for your help you both have saved me allot of time.
You're welcome. In your code above there's a } that's outside the PHP tags btw :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.