same page post request in php isset check not working? - php

Trying to get a simple isset form submit to check if the post request had been clicked from within the same page, but I can't get it working?
<?php
if(isset($_POST['btnSubmit'])){
echo "ok";
}
?>
<form id="form" name="form" action="">
<fieldset>
<legend>
<label for="fullName">Name</label>
</legend>
<input placeholder="Your name" type="text" name="fullName" id="fullName" autofocus>
</fieldset>
<fieldset>
<legend>
<label for="query">Message</label>
</legend>
<textarea placeholder="Type your Message Here...." name="query" id="query"></textarea>
</fieldset>
<input name="btnSubmit" type="submit" id="api-submit" value="Submit" />
</form>
I would expect this just to echo "ok" out onto the page? I've also cut out a lot of unessicary code from my actual work, as im just testing the principle here. But I think I musn't understand how it works, as it is sending the form contents to the URL, but just I don't know how to run seperate php code after the submit button had been clicked.

In your form you haven't specified that it is a POST request and by default, the method will be GET. You'll need to add the method="POST" to your form
<form id="form" name="form" action="" method="POST">
<fieldset>
<legend>
<label for="fullName">Name</label>
</legend>
<input placeholder="Your name" type="text" name="fullName" id="fullName" autofocus>
</fieldset>
<fieldset>
<legend>
<label for="query">Message</label>
</legend>
<textarea placeholder="Type your Message Here...." name="query" id="query">
</textarea>
</fieldset>
<input name="btnSubmit" type="submit" id="api-submit" value="Submit" />
</form>

Related

How to make a form disappear after submit in Wordpress plugin

I am building a WordPress plugin for my livechat. When someone downloads the plugin, I want them to fill out some information (name, e-mail, etc). After submitting that info, the form has to disappear/hide. For some reason I am not successful and imo I've tried everything. At the moment I'm trying to do it with an if-statement checking if the submit-button isset(). Unfortunately that didn't work.
Can someone please help me? The code for display the form and the page after submitting:
<?php
public function display_plugin_setup_page()
{
if (isset($_POST['submitForm'])) {
?>
<form action="options.php" method="post">
<?php
settings_fields('mister_chat_options');
do_settings_sections($this->plugin_name); ?>
<input name="submit" class="button button-primary" type="submit" value="<?php esc_attr_e('Save'); ?>" />
</form>
<?php
} else {
// create the form
?>
<form method="post" action="sendmail.php">
<input type="hidden" name="formSent">
<fieldset>
<input placeholder="Voornaam" type="text" id="vnaam" name="vnaam">
</fieldset>
<fieldset>
<input placeholder="Achternaam" type="text" id="anaam" name="anaam">
</fieldset>
<fieldset>
<input placeholder="Bedrijfsnaam" type="text" id="bnaam" name="bnaam">
</fieldset>
<fieldset>
<input placeholder="E-mailadres" type="email" id="email" name="email">
</fieldset>
<fieldset>
<input placeholder="Telefoonnummer" type="tel" id="telef" name="telef">
</fieldset>
<fieldset>
<input type="submit" name="submitForm" id="contact-submit" data-submit="...Verzenden">
</fieldset>
</form>
<?php
}
}
I placed the sendmail.php file inside the file above and that fixed my problem.

How to Append/Insert new hidden input into form PHP

I have a form request in different page of my script. Example like this one
<form action="/action_page.php" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname">
<input type="submit" value="Submit">
</form>
I want to add a hidden input in my form so it will be like this
<form action="/action_page.php" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<input type="hidden" id="id" name="id" value="randomnumber">
<input type="submit" value="Submit">
</form>
How to do this in PHP after page successfully access, and not inside each html, so I can confirm that each form of my page has input hidden id
<input type="hidden" id="id" name="id" value="randomnumber">
Found a way,
In my last code before html show, I have to add this
echo '<script>var form_id = document.createElement("input");
form_id.setAttribute("type", "hidden");
form_id.setAttribute("name", "form_id");
form_id.setAttribute("value", "form_id");
document.querySelector("form").appendChild(form_id);</script>';
Hope this help other, better than just make sense

Values sent to PHP via POST are all empty strings

I have the following form:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" id="name">
<INPUT type="text" id="id">
<INPUT type="submit" value="Send">
</P>
</FORM>
Unfortunately, whatever is typed into the text fields, the PHP script receives only empty strings in every $_POST variable. All of the variables are set, just empty. What can be the cause of that?
You are missing the name properties in your html:
<FORM action="http://url.to.mysite.com/doc.php" method="post">
<INPUT type="text" name="name" id="name">
<INPUT type="text" name="id" id="id">
<INPUT type="submit" value="Send">
</FORM>
Only form elements with a name attribute will be sent to the PHP script (same with POST)
Try this:
<form action="http://url.to.mysite.com/doc.php" method="post">
<input type="text" id="name" name="name">
<input type="text" id="id" name="id">
<input type="submit" value="Send">
</form>
You need to add the name attribute to your input tags. Example: <input type="text" name="name" id="name" />

Pressing Enter on a <input> submits the wrong <form> (2 forms on 1 page)

I have got two different forms on the same page.
<form action="" method="post" onsubmit="ajax_send('<?print $userid;?>',this.msg.value); return false;">
<input id="msg" autocomplete="off" type="text" name="msg" stlye="width: 80px;" autofocus="autofocus" placeholder="Chat here" />
<input type="submit" />
</form>
and
<form action="?page=about&id=0#comments" method="post">
<textarea class="editbox" id="exitbox" placeholder="Write a comment ..." name="newcomment"></textarea>
<input type="submit" id="submit1" name="submit1"></div></div>
Now when I enter something in <input id="msg"..> and press enter, it submits the second form instead of the first one.
What am I missing?
Your second form isn't closed.
Your second form should by like that
<form action="?page=about&id=0#comments" method="post">
<textarea class="editbox" id="exitbox" placeholder="Write a comment ..." name="newcomment"></textarea>
<input type="submit" id="submit1" name="submit1"></div></div>
</form>

Passing php form info to javascript form

I hope someone can help me. I'm a little at loss on how I can achive this: I'm trying to pass infos generated in my php to another form made in Javascript. For example, if I put my first name in the input field and click submit, then it would go to another page with the actual form and have the first name already filled there.
One thing that I did notice was that the javascript form isn't within the <form> tag, it's in a bunch of tables with some input fields. I can post what it looks like in pastebin if this can help in understanding what I mean.
Also with this script im unable to edit it, I did not make it, it is one of those script you just place on your site thats auto generated.
My form looks like this:
<form action="auto-form/index.php" method="post" name="openleads" onsubmit="return checkForm(this);">
<label for="first">First Name <span class="required">*</span></label>
<input id="first_name" type="text" name="first" applicationforms="true" /><br>
<label class="error" for="name" id="first_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Zip <span class="required">*</span></span>
<input id="zip" type="text" name="zip" applicationforms="true" /><br>
<label class="error" for="name" id="zip_error" style="color:#F00; font-size:11px;">This field is required.</label>
<label for="last">Last Name <span class="required">*</span></label>
<input id="last_name" type="text" name="last" applicationforms="true" /><br>
<label class="error" for="name" id="last_name_error" style="color:#F00; font-size:11px;">This field is required.</label>
<span class="fields">Email <span class="required">*</span></span>
<input id="email" type="text" name="email" applicationforms="true" /><br>
<label class="error" for="name" id="email_error" style="color:#F00; font-size:11px;">This field is required.</label>
<input class="button" type="submit" name="send" value="Send" />
</form>
Any help is appreciated; like I said, I'm a bit at loss on what to do with this one.
php-form.php
<form action="javascript-form.php" method="post">
<input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
javascript-form.php
<form action="" method="">
<input type="text" name="name" value="<?= (isset($_POST['name'])?htmlentities($_POST['name'],ENT_QUOTES):''); ?>" />
<input type="submit" value="Submit" />
</form>
Use PHP to output the POSTed values in to the value attribute of the form fields. You can also use GET variables and use javascript to parse the window.location and scrape those form values.
this seemed to get this done
<script type="text/javascript" src="http://jquery.offput.ca/js/jquery.timers.js"></script>
<script>
$(document).everyTime(1000,function(i){
if($('#ui-datepicker-div').length>0)
{
$('#first_name').val('<?php echo $_POST['first_name']; ?>');
$('#last_name').val('<?php echo $_POST['last']; ?>');
$('#zip').val('<?php echo $_POST['zip']; ?>');
$('#email').val('<?php echo $_POST['email']; ?>');
}
})
$('#first_name').val('test');
</script>

Categories