Jump to content

PHP scripts WITH ARGUMENTS from a button


basicxman

Recommended Posts

OK, here's the run down. I have a database that contains a news table. Each record of the news table has title, and news, and points (plus date, time, approval but they are irrelevant to my question)I've made a PHP script to list all the news records on a page and each record is shown 'Title, submitted on date' then <br /> and it shows the news.What I want to do though is have two buttons, a true/like and then a false/dislike button. When you click the true button it adds a point and then when you click the false button it subtracts a point. So I was going to add a form with the two buttons in an echo and place it beside each recordthe forms action would be <?=$_SERVER['PHP_SELF'];?> and a script on the same page would use if(isset...) on each button and then execute the MySql command(s) and set cookies so the same user cannot choose againalthough the thing is, I need to know which record of news im setting points for, how can i do this? i.e. if a user clicks true beside the news entitled 'Robot saves life' I need to add a point to the 'Robot saves life' entry and not another one on the page.

Link to comment
Share on other sites

Hello there,I am not sure I understand you correctly. But if I understand your problem then a suggestion would be to name the forms in such a way that from the name of the form(or that of the input button) you could instantly extract the name of the entry for which you need to update the points field.

Link to comment
Share on other sites

(Fukushousha, I think he means like Stumbleupon, Digg, Twitter, etc. - except for a single news site rather than the entire web. So the user tells if they liked a news article by clicking Yes or No, helping the site to serve them.)First, would the user ever want to vote more than once on one item (in order to manipulate your database)? Because it would be as easy as deleting your cookie and voting again.Will more than one news items be present in a page? If not, every below occurrence of 1316 is unnecessary.Anyway, I think you'll want to use AJAX because you'll need JavaScript anyway if you don't want to interrupt the user to record his vote. Not using PHP isn't an option because you need to get to the database. To use AJAX, make the two options links, like this:

<a href="java script: approve(1316);">This is a great report!</a> | <a href="java script: reprove(1316);">Come on, you can do better than that!</a>

Make 'approve' and 'reprove' 1) send an async request to your server to count the vote and 2) store the vote in a cookie. See http://w3schools.com/ajax/default.asp for details on that. (We will help you put it together, but we should let you try first.)If you really want to solve this without JavaScript, you can make the two options either:

  • uniquely named submit buttons (e.g. "1316y" and "1316n" for #1316), or
  • uniquely named radio buttons (as above) with one or more submit buttons available.

Link to comment
Share on other sites

Technically the site is for robotics enthusaists. People can post news, it goes through an admin queue but then I had the thought->what if the news is false? So I decided on other users being able to rate the news are true or false,enough false votes and the news goes down (i can do that manually or through a script doesnt matter to me)the news page lists every approved news record in the table of the databasei need the PHP script to generate the javascript code based on the title of the news though, i guess i could do some javascriptwhen you say 1316 could i use a PHP variable insetead like $ROW['title']? thats the only way i see this working because the PHP script has to generate the code automaticallyAJAX is not *required* because i do not need it to load on the same page, im 14 years old so I might integrate that later when my web dev techniques have improved and at least when the sites done

Link to comment
Share on other sites

Technically the site is for robotics enthusaists. People can post news, it goes through an admin queue but then I had the thought->what if the news is false? So I decided on other users being able to rate the news are true or false,enough false votes and the news goes down (i can do that manually or through a script doesnt matter to me)the news page lists every approved news record in the table of the databasei need the PHP script to generate the javascript code based on the title of the news though, i guess i could do some javascriptwhen you say 1316 could i use a PHP variable insetead like $ROW['title']? thats the only way i see this working because the PHP script has to generate the code automaticallyAJAX is not *required* because i do not need it to load on the same page, im 14 years old so I might integrate that later when my web dev techniques have improved and at least when the sites done
or can i send data with the action URL? like in a form with two buttons, action="submitrating.php?x=$ROW['title']" would that work?
Link to comment
Share on other sites

If you are using forms could you not use a hidden field: <input type="hidden" name="newsitem" value="..." /> where value would hold some sort of unique ID. Then you can use $_POST['newitem'] to find the value in the usual way.

Link to comment
Share on other sites

[edit]

Technically the site is for robotics enthusaists. People can post news, it goes through an admin queue but then I had the thought->what if the news is false? So I decided on other users being able to rate the news are true or false,enough false votes and the news goes down (i can do that manually or through a script doesnt matter to me)
If someone were to intentionally submit a false story, they could repeatedly delete their cookie and vote True. The only way to avoid this problem is to supply password protection and only allow members to vote - then store the votes in a database so the users can't make you forget them.[/edit]
the news page lists every approved news record in the table of the database
Every news item on one (potentially huge) page? You won't even divide them into multiple pages like these forum threads or a search engine's results? Some people still use dial-up because it's either less expensive or, as in my parents' case, the only internet service available in a certain location. Such people would give up loading that page long before it loaded completely if you get a good number of records.
when you say 1316 could i use a PHP variable insetead like $ROW['title']? thats the only way i see this working because the PHP script has to generate the code automatically
or can i send data with the action URL? like in a form with two buttons, action="submitrating.php?x=$ROW['title']" would that work?
Use the news item's unique ID number (typically the auto-incremented Primary Key in MySQL) from the database. This is just to prepare for the odd chance that two articles have the same title. (You could make the title unique and give an error page when someone tried to submit a duplicate title, but I would prefer an ID number if I were in your position.)
i need the PHP script to generate the javascript code based on the title of the news though, i guess i could do some javascript...AJAX is not *required* because i do not need it to load on the same page, im 14 years old so I might integrate that later when my web dev techniques have improved and at least when the sites done
No, JavaScript (including AJAX) is not required, but it would definitely smooth your page's operation.EDIT: After thinking on it, I've realized that, because they would have identical logic, 'approve' and 'reprove' would be better as one function 'vote', which would look like this:
vote(1316, true);

And that would also allow the input to be a checkbox and button (or link). (A checkbox and submit button would work without JavaScript.)EDIT2:

If you are using forms could you not use a hidden field: <input type="hidden" name="newsitem" value="..." /> where value would hold some sort of unique ID. Then you can use $_POST['newitem'] to find the value in the usual way.
Yes, that would be a neater way to do it, although it would require that each article be enclosed in its own form. I was thinking of the page as one form, but that's probably not the best way to do it.
Link to comment
Share on other sites

Yes, you can send variables in the URL with a form using post.
wonderful. one thing though, how do i read those variables, are they treated like action.php?x=$ROW['title']then:$y = $x$x being from the title?
Link to comment
Share on other sites

POST variables are available through $_POST['name']. GET vars can be found in $_GET['name']. E.g. action.php?x=102

$_GET['x']; // = 102

Link to comment
Share on other sites

thanks for your help everyone, to answer some questions:the news will not all be on one page, just said that to make the post easier :) i will be using sessions to stop more than one votei am stopping news with the same title as it's 'news' there shoudnt be a duplicatei will be most likely using the hidden input form way of doing things as i had thought of a rough way of doing that before but thanks you guys I now fully know-howthanks again for all your help

Link to comment
Share on other sites

EDIT2: Yes, that would be a neater way to do it, although it would require that each article be enclosed in its own form. I was thinking of the page as one form, but that's probably not the best way to do it.
Doesn't matter if there's more than one form. Each one can have the same method="nextpage.php". If you are calling a number of articles from a database, you will end up displaying them using some sort of loop. Within the loop you will be setting the layout, the title of the item, the text and the buttons - sticking <form..></form> around each section along with the hidden fields will work just as well.
Link to comment
Share on other sites

Doesn't matter if there's more than one form. Each one can have the same method="nextpage.php". If you are calling a number of articles from a database, you will end up displaying them using some sort of loop. Within the loop you will be setting the layout, the title of the item, the text and the buttons - sticking <form..></form> around each section along with the hidden fields will work just as well.
yes i have a loop that access the table and basically just does echo title echo date echo news and then finally ill have echo form
Link to comment
Share on other sites

Murfit, I meant that separate forms would be necessary to keep the hidden fields straight (unless you include the record's title in the hidden's name, but then you defeat the purpose of using the hidden in the first place).

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...