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:

<inputtype="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

<formmethod='POST'onsubmit='return subForm();'>

and

<scripttype="text/javascript">functionsubForm() 
{
        if(confirm("Are you sure you want to delete this?"))
            returntrue;
       elsereturnfalse;

}
</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><scripttype="text/JavaScript">var xmlhttp;
        if (window.XMLHttpRequest)
            xmlhttp=newXMLHttpRequest();
        else
            xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

        functiondeleteOrganization(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 herealert(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();
        }

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

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