Vikas Prasad
Vikas Prasad

Reputation: 3433

Is POST method used to send user defined inputs only

Well, I have 2 php files. The first one user form.php has an html form to collect user inputs and send those data to other php file mysql_insert.php via POST method.

The problem is that along with 6 user inputs, I also have to transfer one more variable count(as discussed in code below) which is not input from user.

I tired following approach:

user form.php--->

<?php $count=file_get_contents("cnt.txt","r"); $count=$count+1; echo"Welcome!<br/>You are student number $count.<br/>"; ?> <html> <p>Fill in the following information to save your marksheet to the database:</p><br/> <form action="mysql_insert.php" method="POST"> Name:<input type="text" name="name" value=""/><br/><br/> Marks(out of 100)<br/> Subject 1:<input type="text" name="sub1" value=""/><br/> Subject 2:<input type="text" name="sub2" value=""/><br/> Subject 3:<input type="text" name="sub3" value=""/><br/> Subject 4:<input type="text" name="sub4" value=""/><br/> Subject 5:<input type="text" name="sub5" value=""/><br/><br/> <p name="count" value="$count"></p> <input type="submit" value="Submit"/> </form> </html>

I simply named an empty P element as count and set its value as $count to send it along with input variables(correct me if there is something wrong here, I am very novice to php).

And on the receiving end I used following code:

mysql_insert.php--->

<?php
  require("connect.php");
  $name=$_POST['name'];
  $s1=$_POST['sub1'];
  $s2=$_POST['sub2'];
  $s3=$_POST['sub3'];
  $s4=$_POST['sub4'];
  $s5=$_POST['sub5'];
  $count=$_POST['count'];
  .
  .
  .
?>

Now on the line $count=$_POST['count']; the browser is throwing the error:

Notice: Undefined index: count in C:\xampp\htdocs\Vikas-117-PHP\level 3\mysql_insert.php on line 10

It seems the count is not being posted to this file. Please guide me where I am wrong.

P.S.: I can of course use the file_get_contents() in the mysql_insert.php and get the count value directly in this file, but I am considering that way as my last option. So please help if the non user-input variable can be posted via forms???

Thanks a million!!!

Upvotes: 0

Views: 968

Answers (7)

krishna
krishna

Reputation: 4099

You can use hidden elements like the following

use

<input type="hidden" value="<?php echo $count; ?>" name="count"/>

instead of

 <p name="count" value="$count"></p>

Upvotes: 1

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

<p name="count" value="$count"></p>

you can not use directly any html tag for post data only use html form fields for posting data. for use html tag values post you need to use js/ajax/php

<p name="count"><?php echo $count;?></p>

or better you use

<textarea name="count"><?php echo $count;?></textarea> 

or for data not showing use hidden field

and get by name this p value or use class or id for get and post data using ajax

Upvotes: 2

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

<p name="count" value="$count"></p>

This is not an input element so you cant post directly. You can however use jquery to get the value and POST using js.

You need to have the value inside input type text or hidden if you want to do the way you are doing now.

Upvotes: 1

Ras4U
Ras4U

Reputation: 492

this line will not work : <p name="count" value="$count"></p>

you can use <input type="hidden" name="count" value="<?php echo $count; ?>" />

Upvotes: 0

Yasin Y&#246;r&#252;k
Yasin Y&#246;r&#252;k

Reputation: 1539

Change this line : <p name="count" value="$count"></p> to the : <input type="hidden" name="count" value="<?=$count?>" />

Upvotes: 1

SajithNair
SajithNair

Reputation: 3867

Instead of

<p name="count" value="$count"></p>

Use

<input type="hidden" name="count" value="<?php echo $count;?>">

Upvotes: 0

Nishant Solanki
Nishant Solanki

Reputation: 2128

you have to put your $count variable in a hidden field in your form.. The data which are put in form inputs only get posted in form submit..

<input type="hidden" name="count" value="<?php echo $count ?>">

Upvotes: 2

Related Questions