-2

Hi all Im a new learner for web development.

I have a page with a form after user click the submit button, then button will submit the form content to the next page to execute and save into database.

So that, my page will go to another page to execute and return to the landing page.

Eg: index.php and exec.php

index.php:

<form name="g-form" action="gbtn-exec.php" method="post" class="goat-vote" onsubmit="return validategForm()">
<input type="text" name="g-product" placeholder="Brand / Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" />


<p class="g-question">Why you love it?</p>

<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea>

<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"></form>

exec.php

if ($_POST["g-product"] && $_POST["g-reason"] != "" )
{
$gproduct = $_POST["g-product"];
$greason =  $_POST["g-reason"];

$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')";
$result = mysql_query($insert,$con);
echo "<script>";
echo "alert('Thank you. Your vote has been recorded.');";
echo "window.location.href='index.php';";
echo "</script>";
}

My question is, how do I use ajax to run exec.php in background? Thanks!

youaremysunshine
  • 341
  • 6
  • 16
  • 28
  • 1
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 23 '13 at 08:57
  • *Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)* — There isn't a shortage of [tutorials](https://duckduckgo.com/?q=ajax+tutorials) – Quentin Sep 23 '13 at 08:58
  • In form submit handler, prevent default behaviour and call your PHP script using ajax. Tutorials on ajax can be find quite easily, no?! – A. Wolff Sep 23 '13 at 08:58

1 Answers1

3

As you tagged jQuery - simply send a HTTP request to the exec.php file:

$('.vote-btn').on('click', function() {
    $.ajax({
      url: "exec.php",
      data: { g-product: $('#g-product').val(), g-reason: $('#g-reasons').val() }
    }).done(function() {
      alert('Thank you. Your vote has been recorded.');
      window.location.href='index.php';
    });
});
Sergey Telshevsky
  • 12,077
  • 6
  • 55
  • 78