0

so i am trying to change my current design for an IoT porject i am experimenting with. currently i am using "submit" as my button type but i don't like how the page refreshes. so i did some digging online and found that ajax was my solution using the "button" type. however i am very unfamiliar with jquery or ajax so im trying to do this.

currently this is my php function that captures the submit button.

if(isset($_POST['button']))
{
    $id = $_POST['button'];
    $sql = mysql_query("SELECT * FROM device WHERE Username = '".$_SESSION['Username']."' && DeviceID = '".$id."'");
    $productCount = mysql_num_rows($sql);
    if ($productCount > 0) 
    {
        $row = mysql_fetch_array($sql);
        $status = $row["Status"];
        $et = "";
        if($status == 1)
        {
            $et = "a";
            $status = 0;
        }
        else
        {
            $status = 1;
        }
        exec("sudo python /var/www/html/light/script'".$id."''".$et."'.py");
        mysql_query("UPDATE device SET Status = '$status' WHERE DeviceID = '$id'");
        header( "refresh:0.01;" );
    //ADD scripts here and also to capture data.
    }
}

ignore the header(refresh) feature i was just trying to do a work around but nothing was satisfying my itch.

Any help or tips are greatly appreaciated

EDIT

also this is the button i have atm

<div class="col-md-2">
            <button type="submit" name="button" id="button" value="'.$id.'" class="btn btn-default">Toggle '.$ddt.'</button>
        </div>
  • You seems to ask question regarding client side behaviour but have posted only server side code, it doesn't really help... So is your question: `How to submit form using ajax`??? `i don't like how the page refreshes` How does it refresh then? What is your expected behaviour? Etc... – A. Wolff Oct 24 '15 at 09:11
  • @A.Wolff oh the python script is server side. because currently the HTML button is type = "submit" which will submit the form and has that refresh look to it. basically i just want the site to not submit but still execute the python script and update the db. if that is possible – adam danial Oct 24 '15 at 09:22
  • Ya, so submit form using ajax. There are already many dupes regarding it, e.g: http://stackoverflow.com/questions/16323360/submitting-html-form-using-jquery-ajax As a side note, your posted code has nothing to do with python, that's PHP... – A. Wolff Oct 24 '15 at 09:24
  • http://stackoverflow.com/questions/16323360/submitting-html-form-using-jquery-ajax – Jeroen Bellemans Oct 24 '15 at 09:24
  • @JeroenBellemans Oops sorry, was just editing comment... :) And as i already voted to close this question, cannot close it as dupe – A. Wolff Oct 24 '15 at 09:24
  • I agree, you should use ajax if you don't want a full-page reload. – piry Oct 24 '15 at 09:25

1 Answers1

0

you can use this post it may have what you are looking for and may help you solve your problem just check it out it seems to be talking about the same thing you are looking to do Post link

Community
  • 1
  • 1
RyanM
  • 718
  • 5
  • 17