0

I have a simple PHP question.

I need to add inside a DB the result of this JavaScript code

function test() {
        $('#percentage').html(Math.floor((Math.random() * 100 + 1)) + '%');
    };

that generate inside a div a random number. I have a couple of textbox with some other infos and hey work properly, i just need to add this one but i can't.

the code to add the other results and then to display them is this:

ADDING ELEMENTS TO DB:

include('dbconnection.php');

$Name = $_POST['name'];
$Surname = $_POST['surname'];
$Random = $_POST['random'];
$Sentence =$_POST['sentence'];

$sql = "INSERT INTO `results`(`Name`, `Surname`, `Random`, `Sentences`) VALUES('$Name', '$Surname','$Random', '$Sentence')";

$retval = mysql_query( $sql );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}

header('Location: index.php');

DISPLAY DB RESULTS:

include('dbconnection.php');

         $result = mysql_query("SELECT `Name`, `Surname`, `Random`, `Sentences` FROM `results`");

         while ($row = mysql_fetch_array($result))
         {
            $name = $row['Name'];
            $surname = $row['Surname'];
            $random = $row['Random'];

            echo"Name: $name -- Surname: $surname -- Random: $random</br>";}

thanks!

2

2 Answers 2

1

You need to have a hidden variable in your form that keeps the value of number and then using this variable you can insert values in yoyr db when your form is posted.

<input type="hidden" id="gen_value" name="gen_value" value=""/>

and in the js function you need to change

function test() {
        var calculatedValue=Math.floor((Math.random() * 100 + 1);
        $('#percentage').html(calculatedValue+'%');
        $('#gen_value').val(calculatedValue + '%');
    };

changes in dbinsert code:

  include('dbconnection.php');

    $Name = $_POST['name'];
    $Surname = $_POST['surname'];
    $Random = $_POST['random'];
    $Sentence =$_POST['sentence'];
    $perCentage=$_POST['gen_value'];

    $sql = "INSERT INTO `results`(`Name`, `Surname`, `Random`, `Sentences`,`Percentage`) VALUES('$Name', '$Surname','$Random', '$Sentence',`$perCentage`)";

    $retval = mysql_query( $sql );
    if(! $retval )
    {
      die('Could not enter data: ' . mysql_error());
    }

    header('Location: index.php');
Sign up to request clarification or add additional context in comments.

Comments

0

You simply follow bellow steps :

add hidden input type in your form

<input type="hidden" name="your_field" id="your_field" class="your_field" >

add below if you want to display in form.

function test() {
  var getValues = Math.floor((Math.random() * 100 + 1));
  $('#your_field').val(getValues + '%');

  /*if your create input type text and give it name "percentage" then use below line.otherwise don't copy it.*/

  $('#percentage').html(getValues + '%');

};

And in your php file get value by using this and then put $field_name in your insert query.

$field_name = $_POST['your_field'];

and then you can use your field value to display result if it have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.