Jump to content
toonz

Click a form image, run a query

Recommended Posts

I am trying to have an UPDATE query run when I click an image on a web page. I just need to have the image call the function to run the mysql from the corresponding php file. The image is a form object, which I need the php function to recognize when it is clicked, and then run the query(ies) within. I don't really know much php, so the php code is based on a snippet I have from one of my other sites, which works fine over there, but I am missing something here that is keeping it from doing the same here.

The page already loads and displays all values in a list associated with $show_id, so that variable is already being pulled from the web page in to the php code. I'm not going to paste the entire books.php file here as there are too many things it does which doesnt have anything to do with this little add-on I am making. I just need to be able to pull that variable in to the query below and have it run when I click on the icon associated with it. The php file also already has the proper config and settings files linked to it that connect to the database, so I do not need to set that up here. It is just a form, calling a function, and running the query. It is also not visible to the user side. It is in an admin page, only visible and accessible to me.

This is code I have in the php file:

    function UpdateAllShowBooks()
    {
    if(strlen(($_POST['retired'])) > 0){

    $sql = "UPDATE ttb_books SET status_id = '2' WHERE show_id='$show_id'";
    $db = new database();
    $db->myquery($sql, 1);
    header('Location:books.php?show_id=$show_id');
    die();
        }
        
    if(strlen(($_POST['upcoming'])) > 0){

    $sql = "UPDATE ttb_books SET status_id = '37' WHERE show_id='$show_id'";
    $db = new database();
    $db->myquery($sql, 1);
    header('Location:books.php?show_id=$show_id');
    die();
        }
        
    if(strlen(($_POST['current'])) > 0){

    $sql = "UPDATE ttb_books SET status_id = '1' WHERE show_id='$show_id'";
    $db = new database();
    $db->myquery($sql, 1);
    header('Location:books.php?show_id=$show_id');
    die();
        }
    }

This is the current image/form html:

              <center><table width=25%>
              <tr><td valign="top" align="center">
              <!--<form method="post" action="books.php?show_id={show_id}">-->
              <form method="post" action="books.php" name="retired">
              <input type="image" src="../images/icon-Red_Light.png" width="20" name="retired" alt="Set status to Out of Print" title="Set status to Out of Print" onclick="return confirm('Set ALL status to Out of Print?')">
              <input name="shid1" type="hidden" id="shid1" value="{show_id}" />
              </form>
              </td><td valign="top" align="center">
              <form method="post" action="books.php" name="upcoming">
              <input type="image" src="../images/icon-Yellow_Light.png" width="20" name="upcoming" alt="Set status to Upcoming Release" title="Set status to Upcoming Release" onclick="return confirm('Set ALL status to Upcoming Release?')">
              <input name="shid2" type="hidden" id="shid2" value="{show_id}" />
              </form>
              </td><td valign="top" align="center">
              <form method="post" action="books.php" name="current">
              <input type="image" src="../images/icon-Green_Light.png" width="20" name="current" alt="Set status to In Print" title="Set status to In Print" onclick="return confirm('Set ALL status to In Print?')">
              <input name="shid3" type="hidden" id="shid3" value="{show_id}" />
              </form>
              </td></tr>
              </table></center>

So clicking on the form image would call the function with that mysql string and update all items in the list accordingly. At this time, when I click the image, it reloads the page fine, but it is not activating the sql in the function and making the Update occur. Thank you for any help you can provide. I really appreciate it.

Share this post


Link to post
Share on other sites

Dump out the contents of $_POST to see what values are actually being sent through the form. It won't be what you expect.

Share this post


Link to post
Share on other sites
1 minute ago, requinix said:

Dump out the contents of $_POST to see what values are actually being sent through the form. It won't be what you expect.

I tried using echo before in the html file but it wasnt loading anything. How do I display the POST value so I can actually see it when I reload the html file in the browser? Thank you

Share this post


Link to post
Share on other sites

Use a function like print_r or var_dump on $_POST, then do a View Source of the page so you can see the raw output (which won't be nicely formatted HTML... probably).

Share this post


Link to post
Share on other sites
10 minutes ago, requinix said:

Use a function like print_r or var_dump on $_POST, then do a View Source of the page so you can see the raw output (which won't be nicely formatted HTML... probably).

I tried using this above the form in the html file

 

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

but it displays on the page like this on reload, so it isnt recognizing it as php code

"; print_r($_POST); echo ""; ?>

 

what changes do I need to make to my original code in order for it to be posting properly to the underlying function?

Share this post


Link to post
Share on other sites

Did you put it in books.php? That's where it needs to be, since that's where the form is going.

Share this post


Link to post
Share on other sites
15 minutes ago, requinix said:

Did you put it in books.php? That's where it needs to be, since that's where the form is going.

The php function already doesnt recognize the form POST, so if I don't use the <?php  ?> in the HTML file itself, nothing would even register and display

Share this post


Link to post
Share on other sites

Wherever you put this new PHP code that didn't work, you can't put it there because it doesn't support PHP code.

It needs to go in books.php. Yes I know the stuff to detect that the image was clicked isn't working, and in order to figure out why I'm saying you need to have that new bit of code run. So put it inside UpdateAllShowBooks, but not inside any of those if blocks. Then you try clicking the image like normal, except this time you should get some output that shows you the POST array. It still won't work. Don't care, just want the output for now.

If UpdateAllShowBooks isn't running at all then you have some other problem, but I don't think that's the case.

Share this post


Link to post
Share on other sites
Posted (edited)
17 minutes ago, requinix said:

Wherever you put this new PHP code that didn't work, you can't put it there because it doesn't support PHP code.

It needs to go in books.php. Yes I know the stuff to detect that the image was clicked isn't working, and in order to figure out why I'm saying you need to have that new bit of code run. So put it inside UpdateAllShowBooks, but not inside any of those if blocks. Then you try clicking the image like normal, except this time you should get some output that shows you the POST array. It still won't work. Don't care, just want the output for now.

If UpdateAllShowBooks isn't running at all then you have some other problem, but I don't think that's the case.

Just tried putting it in the function, but not in one of the IF statements. Page doesnt load now; HTTP ERROR 500

 

EDIT: I got the page to load with it in the function now, but there is no output displayed after I click on a POST image

 

Edited by toonz

Share this post


Link to post
Share on other sites

So  now show us the code immediately surrounding this new code that is supposed to output the post data so we can see if you did it correctly

For future reference - when advised to make coding changes it is important to show us what you did so we don't have to ask.

Share this post


Link to post
Share on other sites

Eh, alright.

You know how normal inputs with names, those names are what show up in $_POST? That's not the case for image inputs because what the browser sends is not the input name but instead where inside the image that you clicked, as name.x and name.y. PHP translates those names into name_x and name_y. So you need to be looking for like retired_x.

But, better would be to not use an image input. It's just weird. Use a submit <button> and put an <img> inside it. If you weren't aware, <button> is like an <input> button except it's much better because it allows you to do things like have stuff on the button (such as an image) instead of just text.

Share this post


Link to post
Share on other sites
36 minutes ago, ginerjm said:

So  now show us the code immediately surrounding this new code that is supposed to output the post data so we can see if you did it correctly

For future reference - when advised to make coding changes it is important to show us what you did so we don't have to ask.

This is what it looks like now

 

	function UpdateAllShowBooks()
	{
	echo "<pre>"; print_r($_POST); echo "</pre>";
			
	if(strlen(($_POST['retired'])) > 0){
		
	$sql = "UPDATE ttb_books SET status_id = '2' WHERE show_id='$show_id'";
	$db = new database();
	$db->myquery($sql, 1);
	header('Location:books.php?show_id=$show_id');
	die();
		}
		
	if(strlen(($_POST['upcoming'])) > 0){

	$sql = "UPDATE ttb_books SET status_id = '37' WHERE show_id='$show_id'";
	$db = new database();
	$db->myquery($sql, 1);
	header('Location:books.php?show_id=$show_id');
	die();
		}
		
	if(strlen(($_POST['current'])) > 0){

	$sql = "UPDATE ttb_books SET status_id = '1' WHERE show_id='$show_id'";
	$db = new database();
	$db->myquery($sql, 1);
	header('Location:books.php?show_id=$show_id');
	die();
		}
			
	}

 

Share this post


Link to post
Share on other sites

if you always redirect and die() you will never see the print_r() output.

"$show_id" is not defined inside that function so will be null.

Why don't you have a single POST variable (instead of your current 3 variables) called "status" which can have have a value of 1, 2, or 37? It would save all the repatetive coding.

Share this post


Link to post
Share on other sites

And if you had php error reporting turned on you would see that your echo statement is flawed.  Can't use a semi in the line since that terminates it.

Share this post


Link to post
Share on other sites

The echo statement is actually three statements on one line. I had to look twice.

Share this post


Link to post
Share on other sites

I don't see where you are sending information to any PHP page when clicking an image.

For instance, all of the images you have have an onClick value that pops up a confirm box, that's it.

 onclick="return confirm('Set ALL status to Upcoming Release?')"

And it's not like you can call a PHP function onclick either, so don't think that's what I'm getting at.  You need a JavaScript function that sends an AJAX call to the PHP file.   Just clicking an image will not cause a <form> element to submit.  So, either correct your onclick, or create a JS snippet that handles clicks on images within that form and submits the form.

Share this post


Link to post
Share on other sites

Requinix gave you the proper solution - use a <button type='submit'> tag with your image buried in the button.  Look it up in whatever html resource you have.  It will look pretty much like what you want and it will actually do the submit to your php script that you want to achieve.  And no JS needed.

Share this post


Link to post
Share on other sites
Posted (edited)

Ok, I completely changed the layout and it seems to be dying at the $sql line. Below is the updated code. The echo outputs are showing the exact values I need based on the button being clicked, and the $show_id is capturing the correct value.

 

The form on the html page:

              <center><table width=25%>
              <tr><td valign="top" align="center">

              <!-- ---- retired --- -->
              <form method="post" action="updatethis.php" name="form1">
              <input type="image" src="../images/icon-Red_Light.png" width="20" name="retired" alt="Set status to Out of Print" title="Set status to Out of Print" onclick="return confirm('Set ALL status to Out of Print?')">
              <input name="retired" type="hidden" value="retired,{show_id}">
              </form>
              </td><td valign="top" align="center">

              <!-- --- upcoming --- -->
              <form method="post" action="updatethis.php" name="form2">
              <input type="image" src="../images/icon-Yellow_Light.png" width="20" name="upcoming" alt="Set status to Upcoming Release" title="Set status to Upcoming Release" onclick="return confirm('Set ALL status to Upcoming Release?')">
              <input name="upcoming" type="hidden" value="upcoming,{show_id}">
              </form>
              </td><td valign="top" align="center">

              <!-- --- current --- -->
              <form method="post" action="updatethis.php" name="form3">
              <input type="image" src="../images/icon-Green_Light.png" width="20" name="current" alt="Set status to In Print" title="Set status to In Print" onclick="return confirm('Set ALL status to In Print?')">
              <input name="current" type="hidden" value="current,{show_id}" >
              </form>
              </td></tr>
              </table></center>

 

updatethis.php

 

<?php
//Include Common Files @1-8E58AE89
define("RelativePath", "..");
include(RelativePath . "/Common.php");
include(RelativePath . "/Template.php");
include(RelativePath . "/Sorter.php");
include(RelativePath . "/Navigator.php");
  
//End Include Common Files


if ($sPOST= $_POST['retired']) {
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is retired';

	echo '<p>'. $aFromPOST[0];
	echo '<p>'. $aFromPOST[1];
	
	global $show_id;
	$show_id = trim($aFromPOST[1]);
	
	header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	$sql = "UPDATE ttb_books SET status_id = '2' WHERE show_id='".$show_id."'"; //the code is dying here
	$db = new database();
	$db->myquery($sql, 1);
	//header("Location:books.php?show_id=".$show_id);
	die();

} elseif ($sPOST= $_POST['upcoming']){
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is upcoming';
	
	echo '<p>'. $aFromPOST[0];
	echo '<p>'. $aFromPOST[1];
	
	global $show_id;
	$show_id = trim($aFromPOST[1]);
	
	header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	$sql = "UPDATE ttb_books SET status_id = '37' WHERE show_id='".$show_id."'";
	$db = new database();
	$db->myquery($sql, 1);
	//header("Location:books.php?show_id=".$show_id);
	die();

} elseif ($sPOST= $_POST['current']){
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is current';
	
	echo '<p>'. $aFromPOST[0];
	echo '<p>'. $aFromPOST[1];
	
	global $show_id;
	$show_id = trim($aFromPOST[1]);
	
	header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	$sql = "UPDATE ttb_books SET status_id = '1' WHERE show_id='".$show_id."'";
	$db = new database();
	$db->myquery($sql, 1);
	//header("Location:books.php?show_id=".$show_id);
	die();
}


?>

I only have the header above the sql as a test, to redirect after I see the echo output to verify it sees which of the 3 form buttons I am clicking. They will be commented back out after I get this operational. In the current code, it loads fine, displays the output, then sends me back to where I was after a few seconds. When I comment the first header and echos out and open up the bottom headers, the page won't load and gives a 500 error. So something about the $sql line it crashing it, but I can't see why. I've compared it to tons of other similar statements online and it looks the same. Can someone spot something I am missing? Thank you again for the help.

 

 

Edited by toonz
fixed the code

Share this post


Link to post
Share on other sites

I changed the 'retired' part of the IF statement to 

if ($sPOST= $_POST['retired']) {
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is retired';

	//echo '<p>'. $aFromPOST[0];
	//echo '<p>'. $aFromPOST[1];
	
	global $show_id, $con;
	$show_id = trim($aFromPOST[1]);
	
	//header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	$sql = "UPDATE ttb_books SET status_id = '2' WHERE show_id='".$show_id."'"; //the code is dying here
	
	  if (!mysqli_query($sql,$con)) {  
    die('<p>Error: ' . mysqli_error());  
	}
	
	$db = new database();
	$db->mysqli_query($sql, $con);
	header("Location:books.php?show_id=".$show_id);
	die();

}

 

to test the connection and it's not spitting out any errors in the output. It shows "Error:" with nothing after it, so I can't tell if it's a connection issue or a sql issue

Share this post


Link to post
Share on other sites

What is $sPOST?  

And - a test for equality is done with two = signs, not one.

This line:

	$sql = "UPDATE ttb_books SET status_id = '2' WHERE show_id='" . $show_id. "'" ; //the code is dying here
	

could be written as:

	$sql = "update ttb_books set status_id = '2' where show_id = '$show_id' ";
	

One doesn't need to concatenate the pieces of sql and php when one begins a string with a double quote.  You need to learn that.

This way you remove the problem you created with the extra quote char at the end of your sql statement.

Share this post


Link to post
Share on other sites

I got it! Here is the code that I got to work

<?php
//Include Common Files @1-8E58AE89
define("RelativePath", "..");
include(RelativePath . "/Common.php");
include(RelativePath . "/Template.php");
include(RelativePath . "/Sorter.php");
include(RelativePath . "/Navigator.php");
  
//End Include Common Files

error_reporting(E_ALL); 
ini_set('display_errors', 1);

//echo var_dump($_POST);

// foreach ($_POST as $key => $value) {
//  echo '<p>'.$key.'</p>';
//  foreach($value as $k => $v)
//  {
//  echo '<p>'.$k.'</p>';
//  echo '<p>'.$v.'</p>';
//  echo '<hr />';
//  }
//} 

$con = mysqli_connect("localhost",yada yada yada) or die ("could not connect to mysql");

if ($sPOST= $_POST['retired']) {
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is retired';
	
	global $show_id, $con;
	$show_id = trim($aFromPOST[1]);
	
	//header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	mysqli_query($con,"UPDATE ttb_books SET status_id = '2' WHERE show_id='".$show_id."'");
	mysqli_close($con);
	header("Location:books.php?show_id=".$show_id);
	die();

} elseif ($sPOST= $_POST['upcoming']){
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is upcoming';
		
	global $show_id;
	$show_id = trim($aFromPOST[1]);
	
	//header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	mysqli_query($con,"UPDATE ttb_books SET status_id = '37' WHERE show_id='".$show_id."'");
	mysqli_close($con);
	header("Location:books.php?show_id=".$show_id);
	die();

} elseif ($sPOST= $_POST['current']){
$aFromPOST = explode (',',$sPOST);
//echo var_dump($aFromPOST) .'is current';
		
	global $show_id;
	$show_id = trim($aFromPOST[1]);
	
	//header( "Refresh:3; url=books.php?show_id=".$show_id, true, 303);
	mysqli_query($con,"UPDATE ttb_books SET status_id = '1' WHERE show_id='".$show_id."'");
	mysqli_close($con);
	header("Location:books.php?show_id=".$show_id);
	die();
}


?>

 

Share this post


Link to post
Share on other sites

This line is wrong

if ($sPOST= $_POST['retired'])

You are supposedly asking if the two items are equivalent but without the extra = you are not doing that.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.