Karthik Malla
Karthik Malla

Reputation: 5800

Can't use NAME in form can I use ID instead?

I am working on a project that is developed using Kohana and due to some conditions I have no access to use NAME in the forms instead I have an option to use ID but I tried using the following method which didn't work.

<form action="sendmail.php" method="post">
    <p><input type="text" size="30" style="border-radius:15px; border:2px solid #000; padding:5px;" placeholder="Name" id="contname" /><br />
    <br />
    <input type="text" size="30" style="border-radius:15px; border:2px solid #000; padding:5px;" placeholder="Email" id="contemail" /><br />
    <br />
    <input type="text" size="30" style="border-radius:15px; border:2px solid #000; padding:5px;" placeholder="Subject" id="contsubject" /><br />
    <br />
    <textarea style="border-radius:5px; border:2px solid #000; padding:5px; width:320px; height:120px;" id="contmessage" placeholder="Message"></textarea><br />
    <br />
    <input type="submit" value="SUBMIT" style="background-color:#9377dd; border-radius:10px; padding-top:3px; padding-bottom:3px; padding-left:16px; padding-right:16px;" /></p>
</form>

sendmail.php

<?php
$from = $_POST["contemail"];
$message = $_POST["contname"] . "<br/>". $_POST["contsubject"] . "<br/>" . $_POST["contmessage"];
mail("[email protected]","From contact form",$message,"From: $from\n");
mail("[email protected]","From contact form",$message,"From: $from\n");
header('Location: faq');
?>

Any alternate method please???

Upvotes: 0

Views: 122

Answers (2)

Jignesh Patel
Jignesh Patel

Reputation: 1022

When you post the form then in server side you can access that field by their name. Now you are not using the name and instead of you are using the id then I will suggest you to use javascript ajax method to post the form. This is the only alternative method is available.

Upvotes: 1

Fenton
Fenton

Reputation: 250972

When you post a form, the form is converted into key/value pairs and sent to the server. For example...

<input name="MyName" value="MyValue">

Is sent to the server as

MyName=MyValue

Unless the browser can form a key/value pair it won't send the data - this is true whether you are missing the key (from the name attribute) or a value (for example a checkbox that is not checked).

You could iterate over the form using JavaScript and create a form post using a different attribute (such as your ID), but if you have access to add JavaScript to this form, it would be easier to just add names.

Upvotes: 0

Related Questions