Jump to content

PHP POST


ark

Recommended Posts

Hi!

 

I'm new to scripting and I'm still a learner of PHP so I need help from you, to solve my problem.

 

Here's what I'm doing.

 

I'm doing a page (only a demo) where there are articles. I've decided to practice and make a backstage admin page where I can add, modify or delete articles, and It works fine. Ah, I forgot to tell, I'm using a MySQL database to store the articles.

 

And the problem: I made my admin page, where I have a form which has all the articles, and have a checkbox assigned to each and every one of them. I'm using the POST method to go to a new page where I can edit the article. So, the problem is, I want the main admin page to be able to send more article IDs with POST (if more than one checkbox is checked) and the editorial page could edit multiple articles at the same time. I thought of arrays, and stuff, but I cannot make it work!

 

I'd appreciate any help, but if I can ask, please give me some example.

 

Thank you very much.

Link to comment
https://forums.phpfreaks.com/topic/57227-php-post/
Share on other sites

If I understand you correctly, you want to be able to edit multiple articles/posts at once. Here is a code snippet which shows the use of naming <input>-tags as an array and how to work with them in PHP:

 

<form action="<?PHP echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="id[]"><br>
<input type="text" name="id[]"><br>
<input type="text" name="id[]"><br>

<input type="submit" value="Submit">
</form>

<?php

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

foreach($_POST['id'] as $id){
	echo '<br>Post id: ' . $id . '<br>';
}
}

?>

 

As you can see all the <input>-tags are named as name="id[]". This allows you to work with $_POST['id'] as an array in PHP. The $_POST['id'] array will contain all post data from the input fields named name="id[]".

 

Now you can run over the $_POST['id'] array with a foreach() statement... Since you are new to PHP (you said so your self :)) I'll explain the foreach. It loops through all entries in the array setting $id = $_POST['id']['x'].

 

- - - - - - - - - - - -

 

You could use this in your code by naming each check box as name="id[]"

 

Form for choosing which article to edit

<form>
  Edit article 1:  <input type="checkbox" name="id[]">
  Edit article 2:  <input type="checkbox" name="id[]">
  Edit article 3:  <input type="checkbox" name="id[]">
<form>

 

Script to receive post data and create the editing form

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

foreach($_POST['id'] as $id){

	//Do some mysql to get the post:
                mysql_query("SELECT * FROM table WHERE id=" . $id);

                //Print a form containing the article text
                echo "<input type=\"text\" name=\"title[$id]\" value=\"get title from mysql\">"
                echo "<input type=\"text\" name=\"body[$id]\" value=\"get body/article from mysql\">"
}
}
?>

 

 

This will result in editing fields being printed for each ID you choose. Now you of course need to have a <form></form> surrounding all the code printing the <input> fields because all the edited articles needs to be posted at once :)

 

Hope it helps

 

Link to comment
https://forums.phpfreaks.com/topic/57227-php-post/#findComment-282860
Share on other sites

I agree with you redarrow, it is bad practice to edit multiple articles at once or at least it makes little sense. But I decided not to comment on it because I remember my early PHP days where I did a lot of bad practice stuff. Sometimes it's needed to realize it's bad :)

Link to comment
https://forums.phpfreaks.com/topic/57227-php-post/#findComment-282867
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.