Submit PHP $_POST with AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arggg
    New Member
    • Mar 2008
    • 91

    Submit PHP $_POST with AJAX

    I have a form in PHP that needs to be processed by the same page. I have ajax calling the page and parsing the data however I cannot get the POST data to be sent via AJAX to the PHP Page. I found this site which gives me an idea that you need to setup the $_POST methods as what looks like a GET then you send the information to the PHP page which in turn interprets it as $_POST data.

    However I was wondering is there a way that without me having to put in ALL the names of the forms elements that it can loop through and add the name plus the value itself?

    [code=html]<form method="post" accept="text/plain" action="javascr ipt:LoadData('/admin/edituser/<?=$_REQUEST[2]?>/save');">
    <fieldset>
    <legend>User Details:</legend>
    <table class="EditForm ">
    <tbody>
    <tr><td>Name: </td>
    <td><input type="text" name="name" maxlength="155" value="<?=$resu lts['Full_Name']?>"></td></tr>
    <tr><td>Title :</td>
    <td><input type="text" name="title" maxlength="155" value="<?=$resu lts['Title']?>"></td></tr>
    <tr><td>Email :</td>
    <td><input type="text" name="email" maxlength="155" value="<?=$resu lts['Email'] ?>"></td></tr>
    <tr><td>AD Account:</td>
    <td><input type="text" maxlength="25" name="ad" <?=$results['AD'] ?>></td></tr>
    <tr><td>Recei ve Alerts:</td>
    <td><input type="checkbox" name="alerts" value="1" <?=$func->iif($results['Send_Alerts'],"checked"," ") ?>></td>
    </tr>
    </tbody>
    </table></fieldset><br>
    <fieldset>
    <legend>Acces s Details:</legend>
    <table class="EditForm ">

    <tbody>
    <tr>
    <td><input type="checkbox" name="access[]" value="1" <?=$func->iif($func->inAccess(AL_LO GIN,$results['Access']),"checked"," ") ?>>Login</td>
    <td><input type="checkbox" value="2" name="access[]" <?=$func->iif($func->inAccess(AL_GR APHS,$results['Access']),"checked"," ") ?>>View Graphs</td>
    <td><input type="checkbox" value="4" name="access[]" <?=$func->iif($func->inAccess(AL_TS M_LIST,$results['Access']),"checked"," ") ?>>View Other TSMs<br>
    </td></tr><tr><td><inp ut type="checkbox" name="access[]" value="256" <?=$func->iif($func->inAccess(AL_AD D_USER,$results['Access']),"checked"," ") ?>>Add User<br>
    </td><td><input type="checkbox" value="512" name="access[]" <?=$func->iif($func->inAccess(AL_ED IT_USER,$result s['Access']),"checked"," ") ?>>Edit User<br>
    </td><td><input type="checkbox" value="1024" name="access[]" <?=$func->iif($func->inAccess(AL_RE MOVE_USER,$resu lts['Access']),"checked"," ") ?>>Remove User<br>
    </td></tr><tr><td><inp ut type="checkbox" value="2048" name="access[]" <?=$func->iif($func->inAccess(AL_ED IT_CONFIG,$resu lts['Access']),"checked"," ") ?>>Edit Config<br>
    </td><td><input type="checkbox" name="access[]" value="4096" <?=$func->iif($func->inAccess(AL_CH ANGE_TEMPLATE,$ results['Access']),"checked"," ") ?>>Change Template<br>
    </td><td>
    </td></tr><tr><td colspan="3">Acc ess Level (higher can edit lowers access)<br>
    <input type="radio" class="a_level" name="a_level" value="0" checked>None <input type="radio" class="a_level" name="a_level" value="16384" <?=$func->iif($func->inAccess(AL_LE VEL_1,$results['Access']),"checked"," ") ?>>1 <input type="radio" class="a_level" name="a_level" value="32768" <?=$func->iif($func->inAccess(AL_LE VEL_2,$results['Access']),"checked"," ") ?>>2 <input type="radio" class="a_level" name="a_level" value="65536" <?=$func->iif($func->inAccess(AL_LE VEL_3,$results['Access']),"checked"," ") ?>>3 <input type="radio" class="a_level" name="a_level" value="131072" <?=$func->iif($func->inAccess(AL_LE VEL_4,$results['Access']),"checked"," ") ?>>4 <input type="radio" class="a_level" name="a_level" value="262144" <?=$func->iif($func->inAccess(AL_LE VEL_5,$results['Access']),"checked"," ") ?>>5 <input type="radio" class="a_level" name="a_level" value="524288" <?=$func->iif($func->inAccess(AL_SY STEM_ADMIN,$res ults['Access']),"checked"," ") ?>>System Admin</td></tr>
    </tbody>
    </table></fieldset>
    <br>
    <fieldset>
    <legend>TSM Report Properties:</legend>
    <table>

    <tbody>
    <tr>
    <td>Name:</td>
    <td><input type="text" name="tsm_name" value="<?=$resu lts['Name'] ?>"></td>
    </tr>
    <tr>
    <td>Group:</td>
    <td><select name="tsm_Group ">
    <? foreach($enums as $key)
    {
    echo "<option ".$func->iif($key==$res ults['Group'],'selected','') .">".$key."</option>";
    } ?>
    </select></td>
    </tr>
    <tr>
    <td>Include in reports:</td>
    <td><input type="checkbox" name="tsm_inclu de" value="1" <?=$func->iif($results['Include'],"checked"," ") ?>></td>
    </tr>
    </tbody>
    </table></fieldset>
    <br>
    <table><tr><td> <input type="submit" value="Save" name="UserEdit_ Save"></td><td><a
    href="javascrip t:LoadData('/users');">Cance l</a></td></tr></table></form>
    [/code]
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    You will have to add the elements/values manually. You can loop over all the form elements and check their types and deal with each one accordingly, e.g. checkbox/radio buttons and select elements are dealt with differently from text boxes.

    Comment

    • arggg
      New Member
      • Mar 2008
      • 91

      #3
      Originally posted by acoder
      You will have to add the elements/values manually. You can loop over all the form elements and check their types and deal with each one accordingly, e.g. checkbox/radio buttons and select elements are dealt with differently from text boxes.
      Could you give an example please?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        See this link (serializeForm) .

        Comment

        Working...