I've been asking a lot of questions as of late related to creating RESTful services with PHP. My question is this:
Can all services (GET, POST, PUT, DELETE) be done from a single web form using radio buttons?
This is what I am picturing in my head:
<form action="MyService.php" method="GET">
<label for="username">Username</label>
<input type="text" name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<label for="id">Task ID</label>
<input type="text" name="id" id="id"/><br />
<label for="desc">Task Description</label>
<input type="text" name="desc" id="desc"/><br />
<label>
<input type="radio" name="service" value="getRadio" checked/> GET
</label>
<label>
<input type="radio" name="service" value="postRadio" /> POST<br />
</label>
<input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
<input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
<input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
<input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
<input type="submit" name="submit" value="ACTION"/>
</form>
It's incomplete so far, but what I'm thinking of trying to do is have radio button selections for each service, then outline my php file like this:
$request = $_SERVER["REQUEST_METHOD"]
switch($request) {
case 'GET':
// logic for GET based on radio button selected
break;
case 'POST':
// logic for POST based on radio button selected
break;
// and then PUT and DELETE
}
Is this doable? If so, am I on the right track, or do I need to make changes?
if you want to use the forms, and providing it does work as seems by this spec, then you can use some javascript to change the method of your form onsubmit.
<form action="MyService.php" method="GET" onsubmit='this.method = this.service.value'>
<label for="username">Username</label>
<input type="text" name="username" id="username" /><br />
<label for="password">Password</label>
<input type="password" name="password" id="password" /><br />
<label for="id">Task ID</label>
<input type="text" name="id" id="id"/><br />
<label for="desc">Task Description</label>
<input type="text" name="desc" id="desc"/><br />
<label>
<input type="radio" name="service" value="GET" checked/> GET
</label>
<label>
<input type="radio" name="service" value="POST" /> POST<br />
</label>
<label>
<input type="radio" name="service" value="PUT" /> PUT
</label>
<label>
<input type="radio" name="service" value="DELETE" /> DELETE<br />
</label>
<input type="hidden" name="REQUEST_METHOD" value="GET"/><br />
<input type="hidden" name="REQUEST_METHOD" value="POST"/><br />
<input type="hidden" name="REQUEST_METHOD" value="PUT"/><br />
<input type="hidden" name="REQUEST_METHOD" value="DELETE"/><br />
<input type="submit" name="submit" value="ACTION"/>
</form>
Related
<form action="confirm.php" method="post" name="">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
<br>
<br>
<button type="submit" class="">Submit</button>
</form>
having problem with the radio buttons.
And on the confirm page I have used foreach loop. How do i also get the values for "f_status" ?
See first of all its an interesting question but unfortunately, the fact is HTML can't understand the field without different names if they are in same form.
so the only way to achieve your goal is to put all of them in three different form tags and then u can name all of the same i.e. f_hobby[]
Also, you need to add a single button to submit all three of them. To achieve this u can use onsubmit() or onclick() function.
<form action="confirm.php" method="post" name="" id="form1">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form2">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<form action="confirm.php" method="post" name="" id="form3">
Hobby : <input type="text" name="f_hobby[]" value="" placeholder="Enter your Hobby"/>
Status : <input name="f_status[]" type="radio" value="1" /> ON <input name="f_status[]" type="radio" value="0" /> OFF
</form>
<button type="submit" class="" onclick="submitForms()">Submit</button>
<script>
submitForms = function(){
document.getElementById("form1").submit();
document.getElementById("form2").submit();
document.getElementById("form3").submit();
alert("gajab");
}
</script>
I have given the forms an id to submit it using a single button, u can also use class instead. I am sure this will solve your problem.
I have a form which has the question and 4 answers (it's a quiz), I tried to comment on https://stackoverflow.com/a/29280091/6482242 but I don't have enough reputation yet. This is very similar to what I have (as well as How to set the correct answer via checkbox? ).
The layout that I have is:
<label for="question_text">Question Text:</label>
<textarea title="Insert Question Text" name="question_text"></textarea>
<h4>Check Correct answer -></h4>
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_1">Answer One:</label>
<input type="text" title="Enter Answer" name="answer_1" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_2">Answer Two:</label>
<input type="text" title="Enter Answer" name="answer_2" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_3">Answer Three:</label>
<input type="text" title="Enter Answer" name="answer_3" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_4">Answer Four:</label>
<input type="text" title="Enter Answer" name="answer_4" />
<br />
<br />
<input type="submit" value="Submit Question" name="question_submit" />
There is some PHP in the background to grab these and put them into a database, but I need to know please what the correct syntax is to link a radio button to a text input?
Thanks in advance :D
You should change the values of radio buttons and then store its value to a separate column in table with column something like correct_answer
<label for="question_text">Question Text:</label>
<textarea title="Insert Question Text" name="question_text"></textarea>
<h4>Check Correct answer -></h4>
<input type="radio" name="correct_answer_flag" value="1">
<label for="correct_answer_flag, answer_1">Answer One:</label>
<input type="text" title="Enter Answer" name="answer_1" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="2">
<label for="correct_answer_flag, answer_2">Answer Two:</label>
<input type="text" title="Enter Answer" name="answer_2" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="3">
<label for="correct_answer_flag, answer_3">Answer Three:</label>
<input type="text" title="Enter Answer" name="answer_3" />
<br />
<br />
<input type="radio" name="correct_answer_flag" value="4">
<label for="correct_answer_flag, answer_4">Answer Four:</label>
<input type="text" title="Enter Answer" name="answer_4" />
<br />
<br />
<input type="submit" value="Submit Question" name="question_submit" />
And in that column just store the number of the selected Radio. So with each question, you will be storing 4 options, and among those 4 there will be one correct answer and its index will be stored in a separate table where you store you answer options.
I have this wizard that needs to display the previous posted value
index.html
<form method="post" action="posted.php">
<input type="text" name="surname" value="" placeholder="Surname" />
<input type="text" name="firstname" value="" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
in the posted.php i have a similar form only this time i know the value from $_POST
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" />
<input type="checkbox" name="jquery" />
<input type="checkbox" name="python" />
<input type="submit" value="Submit" />
</form>
I am having a hard time trying to come up with a solution that shows what checkbox was checked.I have seen several solutions like https://stackoverflow.com/a/11424091/1411148 but i wondering if there more solution probably in html5 or jquery.
How can i show what checkbox was checked?.The probem i am having is that <input type="checkbox" name="jquery" checked /> checked checks the checkbox and no post data can be added to show what the user checked.
This would be a way to go:
<form method="post" action="finish.php">
<input type="text" name="surname" value="<?php echo $_POST['surname']; ?>" placeholder="Surname" />
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" placeholder="Firstname" />
<input type="checkbox" name="php" <?php if (isset($_POST['php'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="jquery" <?php if (isset($_POST['jquery'])) echo 'checked="checked"'; ?> />
<input type="checkbox" name="python" <?php if (isset($_POST['python'])) echo 'checked="checked"'; ?> />
<input type="submit" value="Submit" />
</form>
First my code:
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post">
<P>
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</P>
</FORM>
';
?>
So if i run that on my xampp-Server, it shows a "Hello" and the Form in a new line.
What must I do that all this is written in one line?
Thanks
You need to remove the <p> element and display the form inline.
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post" style="display:inline">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
Remove the paragraph break '<P>'
You should really use a heredoc for this type of HTML output in PHP. Technically, the <p> tag should be around the Hello, not the form. You're looking for something like:
<?php
echo <<<EOT
Hello
<FORM ACTION="uebung3.php" METHOD="post">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
EOT;
?>
Even by removing <p> tags, your hello will still appear on a seperate line. This is because it is outside your <form> tag.
Put it inside <form> like this, while removing the <p> tags:
<?php
echo '<FORM ACTION="uebung3.php" METHOD="post">Hello
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
I have a form:
<form action="<?php echo the_permalink(); ?>" method="POST">
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />
<?php echo $firstnameError; ?>
<input type="text" name="email" value="" placeholder="Yoyr Email" />
<?php echo $emailError; ?>
<br>
<input type="text" name="company" value="" placeholder="Your Company Name" />
<input type="text" name="phone" value="" placeholder="Your Phone number" />
<textarea name="project" rows="4" cols="50" placeholder="Describe the scope of work and the most important features of the project *'"></textarea>
<?php echo $addressError; ?>
<br>
<input type="radio" name="budget" value="1500" />
<input type="radio" name="budget" value="2500" />
<input type="radio" name="budget" value="5000" />
<input type="radio" name="budget" value="10000" />
<input type="radio" name="budget" value="100001" />
<input type="radio" name="budget" value="not sure" />
<input type="hidden" name="submit" value="1" />
<input type="submit" value="SUbmit" />
</form>
It actions to the same page, but when I do print_r($_POST); it does not print anything, i.e. no value in $_POST.
What could be the reason(s) for this? I studied a few questions on SO on this but none gave me the answer I was looking for.
If your passing the name as a Post value, wordpress DOSNT like this!
change this
<input type="text" name="name" value="" placeholder="Your First and Last Name *" />
to
<input type="text" name="thename" value="" placeholder="Your First and Last Name *" />
changing the name to thename, Will work guaranteed! ;)
<form action="<?php the_permalink(); ?>" method="POST">
You don't need to echo the_permalink().
This works for me:
<?php print_r($_POST);?>
<form action="" method="POST">
<input type="text" name="name" value="" placeholder="Your First and Last Name *" /><?php echo $firstnameError; ?><input type="text" name="email" value="" placeholder="Yoyr Email"/><?php echo $emailError; ?><br>
<input type="text" name="company" value="" placeholder="Your Company Name"/><input type="text" name="phone" value="" placeholder="Your Phone number"/>
<textarea name="project" rows="4" cols="50"placeholder="Describe the scope of work and the most important features of the project *'"></textarea><?php echo $addressError; ?><br>
<input type="radio" name="budget" value="1500" /><input type="radio" name="budget" value="2500" /><input type="radio" name="budget" value="5000" /><input type="radio" name="budget" value="10000" /><input type="radio" name="budget" value="100001" /><input type="radio" name="budget" value="not sure" />
<input type="hidden" name="submit"value="1"/>
<input type="submit" value="SUbmit" />
</form>
change this
acton="<?php echo the_permalink(); ?>"
to
action="<?php echo the_permalink(); ?>"
I found same problem in my project and solution is in $_POST, maybe you use lower case in your code, change it to upper case.
change $_post to $_POST !
SOLUTION:
due to request problems, in wordpress sites instead of
<form action="http://example.com/">...
You may need to point the .php file.. example:
<form action="http://example.com/index.php">...
//////p.s. echo is automatically done with the_permalink() [same is: echo get_permalink()