Forum Moderators: coopster

Message Too Old, No Replies

PHP forms and Javascript

php forms and javascript

         

xKillswitchx

1:42 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Hello all,
I have a webapp I am developing in PHP and am almsot at a complete loss in dealing with Javascript (I don't know it, but I for the most part, can understand it and enough to edit slightly).

I have a list of content items that is grabbed from a mysql database through a loop in PHP. For each content item, I have a checkbox. want I found I really needed, was the option to change the forms action URL with Javascript, so I dug up a script to do so...

function OndeleteContent()
{
document.contentForm.action = "?task=content&action=delete_content"
document.contentForm.target = "_self"; // Open in a new window
document.contentForm.submit(); // Submit the page
return true;
}

Thats the Javascript I use for deleting items. Should work in conjunction with this ...

<form name="contentForm" method="post" action="">

<input type="image" src="templates/images/delete_content.gif" alt="Delete Content" class="contentOption" name="OndeleteContent" onclick="return OndeleteContent();">

The Javascript and image submit sends me to the appropriate page, but doesnt appear to be sending any of the data with it. I checked to see if the form submittion was set through PHP, else return a message "Not set" and I costantly retrive Not Set message. Not sure if this is a PHP error or something with the Javascript, but here is also a small bit of the PHP...

if (isset($_POST['OndeleteContent']))
{
foreach($_POST['checkbox'] as $id)
{
$sql = "DELETE FROM content WHERE id = '$id'";
$result = mysql_query($sql) or die("Error in Query");
}
}
else {
echo "Not set";

}

Can anyone help diagnose this issue? If it helps, I have other options to edit items, publish, and unpublish items, which are all controlled by similar methods (Javascript functions). I would link to live example, but I am testing locally through XAMPP.

phparion

7:03 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



check

echo "<pre>"; print_r($_POST); print_r($_GET); echo "</pre>";

see what variables you are getting in which array, if you are not sure about using POST or GET in receiving page for a variable then use $_REQUEST instead.

xKillswitchx

3:14 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



hmm ... I used print_r in the else (not set) and it printed off all the correct data. I adjusted the print_r($_POST) to print_r($_POST['OndeleteContent']) and it just showed me the $_GET information. Thanks, helps narrow down the problem.

xKillswitchx

3:22 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



Would it be safe to not specify $_POST['post data'] and instead use if (isset($_POST))? There is only one option this particular function can use and it doesnt do anything if the data isnt set (for now it just says not set).

phparion

4:39 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am a little confused....

If your problem is between GET and POST array then as I told you earlier use $_REQUEST['varName'] this will solve it automatically and will check both POST and GEY arrays.

secondly I saw you are using a queryString kinda URL in javascript to change the form action URL so why dont you put your variable in that URL and then check that variable in the receving page with $_GET as it will be a sure shot to have that in GET since it will be going in the query string?

you can skip using the submit button name with this.

penders

11:36 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<input type="image" src="templates/images/delete_content.gif" alt="Delete Content" class="contentOption" name="OndeleteContent" onclick="return OndeleteContent();">

This looks a bit odd to me, using the same element name as a function name?! Is that valid?! Even if it is, the potential for error/confusion is dramatically increased!