0

I have two pages in php. First page is the following:

<?php

// --- please consider that my form is inside a while statement...


<form action='deletepost.php' method='post' >
  <input type='hidden' name='var' value='$comment_id;'>
  <input type='submit' value='delete' >
</form>

?>

and page deletepost.php is the following:

<?php

    $comment = mysql_real_escape_string($_POST['var']);
    echo" $comment ";

    // --- then starts successfully the delete process I have created...

?>

So first page includes a form button delete that when I press it passes the value that I want to page deletepost.php successfully (I use echo to see it as you can see). I have decided to use a javascript lightbox for my form in first page. I have changed my code to that:

<?php

<a href = javascript:void(0) onclick = document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'><h2><font color=green size=3>Delete All</font></h2></a>
<div id=light class=white_content>


<form action='deletepost.php' method='post' >
  <input type='hidden' name='var' value='$comment_id'>
  <input type='submit' value='delete' onClick=submit(); >
</form>


<a href=javascript:void(0) onclick=document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'><button style='margin-left:250px; width:95px;'>Cancel</button></a>
</div>
<div id=fade class=black_overlay></div>  

the problem is that after using javascript, values are not passed to deletepost.php .Any idea why this might happend?

13
  • 1
    <input type='submit' value='delete' onClick=submit(); > remove onclick event from this line Commented Jan 15, 2014 at 16:50
  • i did it still nothing Commented Jan 15, 2014 at 16:52
  • close the first <?php tag with ?> in next line and use as follow <input type='hidden' name='var' value=<?=$comment_id?>> <input type='submit' value='delete' /> Commented Jan 15, 2014 at 17:00
  • I thing that the problem comes from href of javascript lightbox that probably not allow passing the variable to next page Commented Jan 15, 2014 at 17:03
  • It is outside a form, what is it supossed to do Commented Jan 15, 2014 at 17:04

2 Answers 2

1

I see no point of having javascript over a submit button. You either do it on <a> or <button>, and you should refer to the form name.

<form name='form1' action='deletepost.php' method='post'>

...


<button onclick='document.form1.submit()'>delete</button>

Calling submit() only will do nothing, because there is no such built in alone method. You can aliase the refering in a wrapper method

function submit() {
    document.form1.submit();
}

Here are my tests:

test1.php:

<?php $comment_id = 1 ?>
<?php while($comment_id < 10): ?>
    <a href="javascript:void(0)" onclick="document.getElementById('light<?=$comment_id;?>').style.display='block';document.getElementById('fade<?=$comment_id;?>').style.display='block'"><h2><font color=green size=3>Delete All</font></h2></a>
    <div id="light<?=$comment_id;?>" class="white_content" style="display: none">


    <form action="test2.php" method="post" name="form1">
      <input type="hidden" name="var" value="<?=$comment_id;?>" />
      <button onClick="document.form1.submit();">Delete</button>
    </form>


    <a href="javascript:void(0)" onclick="document.getElementById('light<?=$comment_id;?>').style.display='none';document.getElementById('fade<?=$comment_id;?>').style.display='none'"><button style="margin-left:250px; width:95px;">Cancel</button></a>
    </div>
    <div id=fade class=black_overlay></div>
<?php $comment_id++; ?>
<?php endwhile;?>

It generates me 9 comment lightboxes. On fifth one I click on Delete all it expands the button Delete. Then I click the button and it redirects me to test2.php Where I have

<?php var_dump($_POST); ?>

And the output in the webpage is:

array (size=1)
  'var' => string '5' (length=1)
Sign up to request clarification or add additional context in comments.

5 Comments

I am just using a simple javascript lightbox how is it possible to not allow me passing the var to next page?
I want to pass $comment_id to next page and when using the href javascript for the lightbox it is not passing it
Where does $comment_id comes from? Isn't it just $comment ?
it comes above from my code and it is passed to form at name='var' value='$comment_id'
dear friend thank you for your excellent help...it works thanks to you!
0

I don't think you need the onClick property for your submit input. ESPECIALLY if you don't actually have a submit method defined anywhere in your JS. Setting that attribute could be enough to stop the default submit behavior of the form and will stop your php from running.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.