-2

I'm trying to make a page on php that dynamically insert some events from my sql database, the problem is, when i put it on a HTML, i can't post the id that i have clicked.

Or it means, when i click on a entire div i need to post it whit the id of an input type hidden...

Any tips?

Here is my code:

<?php
if($_POST){
    $sql = mysql_query("SELECT * FROM evento");
    while ($evento = mysql_fetch_object($sql)) {
        if($_POST["id".$evento->id_evento] == $evento->id_evento){
            echo "oi";
        }
    }

}else{
    $sql = mysql_query("SELECT * FROM evento");
    while ($evento = mysql_fetch_object($sql)) {

        echo "<form action='eventos' method='POST'>
                <input type='hidden' name='id' value='".$evento->id_evento."'/>
                <div onclick='javascript: this.submit();' id='id' class='col-md-4' >
                    <div style='white-space: nowrap;text-align: center;min-height:190px'>
                        <span style='display: inline-block;height: 100%;vertical-align: middle;'></span><img src='".$root."img/upload/eventos/".$evento->img."' style='width:100%; max-height:200px; vertical-align: middle;'>
                    </div>
                    <p style='color:#00AEC3;background:#F2F2F4;padding:10px 5px'>" . $evento->titulo . "<br> <span style='color:black'>".date('d/m/Y', strtotime($evento->data_inicio))." à ".date('d/m/Y', strtotime($evento->data_fim))."</span></p>
                </div>
            </form>";
    }  
}
?>

1 Answers1

0
<?php this.id.submit();?>

The above is not valid PHP code. PHP does not use the . operator, I think you are confusing javascript and PHP. PHP uses the -> operator so it would probably be something like the following (if that class exists)..

<?php $this->id->submit(); ?>

However I think you are trying to get at the ID column for the database record and if that is the case the above still will not work because the database record will not be a 3 dimensional object like you are doing with the submit() method. Database results will never have a submit method. Maybe something like the code below will solve your problem.

<?php
$sql = mysql_query("SELECT * FROM evento");
while ($evento = mysql_fetch_object($sql)) {
    echo "<form action='eventos' method='GET'>
      <input type='hidden' name='id' value='".$evento->id_evento."'/>
      <div onclick='<?php $envento->id ;?>' id='id' class='col-md-4' >
           <div style='white-space: nowrap;text-align: center;min-height:190px'>
                <span style='display: inline-block;height: 100%;vertical-align: middle;'></span><img src='".$root."img/upload/eventos/".$evento->img."' style='width:100%; max-height:200px; vertical-align: middle;'>
           </div>
           <p style='color:#00AEC3;background:#F2F2F4;padding:10px 5px'>" . $evento->titulo . "<br> <span style='color:black'>".date('d/m/Y', strtotime($evento->data_inicio))." à ".date('d/m/Y', strtotime($evento->data_fim))."</span></p>
      </div>
 </form>";
}  

My FEAR is that you want some javascript code in that onclick handler while also passing the ID through but I have no idea what you are trying to accomplish here other than putting an ID into an onclick handler. For something like that you could do something like this.

<div onclick='javascriptFunctionHere(<?php $envento->id ;?>)' id='id' class='col-md-4' >
Joseph Crawford
  • 1,470
  • 1
  • 15
  • 29
  • I am still not seeing what you are wishing to accomplish. You are putting this.submit but is there an actual javascript function that you are trying to call or are you trying to submit a form on click? – Joseph Crawford Jun 17 '15 at 00:53
  • I think this might help to answer your question on how to submit a form on click. You first have to get the element like they do with document.getElementById(); http://stackoverflow.com/questions/6799533/how-to-submit-a-form-with-javascript-by-clicking-a-link – Joseph Crawford Jun 17 '15 at 00:55
  • In your case you might be able to do something like the following onclick="document.getElementById("form-id").submit()" – Joseph Crawford Jun 17 '15 at 00:56