Skip to content Skip to sidebar Skip to footer

How Do I Submit A POST Variable With Javascript?

So I am trying to submit a variable and the name of the variable via a form. I switched a button from submit to button because I need additional validation. Anyway, here's the butt

Solution 1:

You can use <input type="hidden">:

echo '<input type="hidden" name="org_id" value="'.$org_id.'" />'

This should render something like:

<input type="hidden" name="org_id" value="1" />

Using this code you can access the hidden field data using:

$org_id = $_POST['org_id'];

Solution 2:

instead use onsubmit

<form method='POST' onsubmit='return subForm();'>

and

<script type="text/javascript">
function subForm() 
{
        if(confirm("Are you sure you want to delete this?"))
            return true;
       else
           return false;

}
</script>

edit: you can also change

if (isset($_POST["del"])  && ($_POST['del'] !== '')) {

to

    if ( !empty($_POST['del']) ) {

but i think this line is your problem

$resfile = mysql_query('SELECT file_loc from organization WHERE org_id = '.$del);

try

$resfile = mysql_query("SELECT file_loc from organization WHERE org_id = '".$del."' ");

Solution 3:

Looking at http://www.w3schools.com/tags/tag_button.asp suggests that the 'name' of a button isn't submitted as opposed to its 'value' or 'text'. Why not use an input of type hidden as just suggested?


Solution 4:

I suggest you rethink using a form at all and consider AJAX. This would solve the problem of knowing which button was clicked and the page doesn't need to reload.

Here is a sample of what you're trying to do:

<html>
<head>
    <script type="text/JavaScript">
        var xmlhttp;
        if (window.XMLHttpRequest)
            xmlhttp=new XMLHttpRequest();
        else
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

        function deleteOrganization(orgID)
        {
            xmlhttp.onreadystatechange = function()
            {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                {
                    // in your PHP file, have it echo something like "SUCCESS" or "FAIL", and the response will be alerted here
                    alert(xmlhttp.responseText);
                    refreshList();  // either call another function to update the list or return the new list after the success/fail response.
                }
            };
            xmlhttp.open("GET", "delete.php?orgID="+ orgID, true);
            // OR - xmlhttp.open("GET", "page.php?action=DELETE&orgID="+ orgID, true);
            xmlhttp.send();
        }

        function refreshList()
        {
            // either delete the row with JavaScript or make another AJAX call to get the new list after the entry was deleted.
        }
    </script>
</head>
<body>
    <button onclick="deleteOrganization('<?php echo $org; ?>')">Delete</button>
</body>
</html>

Post a Comment for "How Do I Submit A POST Variable With Javascript?"