Replace form button whit php regex - php

I have a form an need replace button in my form with another data. I should use php regex and replace it. My form sample this is:
<form name="ialRegister" id="ialRegister" method="post">
<input type="text" name="name" id="name">
<input type="text" name="email" id="email">
<input type="text" name="mobile" id="mobile">
<label data-attr="subtitle" class="smallTxt hidden" for="ialButton10"></label>
<button class="loginBtn ial-submit" name="submit" id="ialButton10">
<span><i class="ial-load" style="visibility: hidden;"></i>
<span data-attr="label">Register</span></span>
</button>
</form>
and I used this regex but it's not working:
$newField = 'custom data';
$form = preg_replace("#(<form.*id=\"ialRegister\".*>.*)
<button.*name=\"submit\".*>.*<\/button>(.*<\/form>)#sU", '$1'.$newField.'$2', $form);
How can do it?

Related

I need a form not to clear the fields during a submit for test mode

I'm writing a php form that has a button to test the database connection before proceeding to the next step. The only problem is that running the test, clears the fields. I can put onsubmit="return false" at the top of the form, but then the test works fine, but I can't submit the form for its real purpose then. I'm guessing this could be fixed with javascript, but I'm a total noob there and I'm wondering if there is a PHP/HTML way to accomplish this.
Here's the form. The test button runs a test pdo connection and gives feedback and the "Next Step >>" button writes the configuration to a file and goes on to the next step.
<H2>Please fill in your database credentials</H2>
<form class="form" action="" onsubmit="return false" method="post">
<label for="dbh">Database Host
<input class="form-control" type="text" name="dbh" value=""></label>
<br><br>
<label for="dbu">Database User
<input class="form-control" type="text" name="dbu" value=""></label>
<br><br>
<label for="dbp">Database Password
<input class="form-control" type="text" name="dbp" value=""></label>
<br><br>
<label for="dbn">Database Name
<input class="form-control" type="text" name="dbn" value=""></label>
<br><br>
<input class="btn btn-success" type="submit" name="test" value="Test Settings">
<input class="btn btn-primary" type="submit" name="submit" value="Next Step >>">
</form>
You need to write in $_POST vars in your inputs.
<label for="dbh">Database Host
<input class="form-control" type="text" name="dbh" value="<? if ($_POST['dbh']){ print $_POST['dbh']; } ?>"></label><br><br>
<label for="dbu">Database User
<input class="form-control" type="text" name="dbu" value="<? if ($_POST['dbu']){ print $_POST['dbu']; } ?>"></label><br><br>
<label for="dbp">Database Password
<input class="form-control" type="text" name="dbp" value="<? if ($_POST['dbp']){ print $_POST['dbp']; } ?>"></label><br><br>
<label for="dbn">Database Name
<input class="form-control" type="text" name="dbn" value="<? if ($_POST['dbn']){ print $_POST['dbn']; } ?>"></label><br><br>

Input string separated by underscore in form

I haven't come to much luck finding anything on this, since I couldn't think how to word it originally.
Basically I have a form, in HTML, the end user will submit a value such as "12345_54321" into 1 input field, then process.
I would like to be able to allow there to be 2 input fields instead, so one of them they would enter "12345" and in the second one, they'd enter "54321".
Which seems easy enough, but my real need is that the "_" must be used as a separator, such as, when the value is submitted, it will process "12345_54321" instead of "12345" and "54321"
My form so far:
<form role="form" method="post" action="process.php">
<fieldset>
<div class="form-group">
<input size="18" type="visible" name="postid" id="postid" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" ><input size="18" type="visible" name="postid" id="postid" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" >
</div>
<input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process"/>
</fieldset>
If you want to display 2 inputs for the story id, you have to modify your page to be like so (no need for the underscore (_):
<form role="form" method="post" action="process.php">
<fieldset>
<div class="form-group">
<input size="18" type="visible" name="story_id_1" id="story_id_1" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" >
<input size="18" type="visible" name="story_id_2" id="story_id_2" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" >
<input size="18" type="visible" name="comment_id" id="comment_1" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" >
</div>
<input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process"/>
</fieldset>
</form>
In your process.php you can get these variables by looking in your POST.
<?php
$story_id_1 = '';
$story_id_2 = '';
$comment_id = '';
// Check for empty fields
if(isset($_POST['story_id_1']))
$story_id_1 = $_POST['story_id_1']; // From HTML Page
if(isset($_POST['story_id_2']))
$story_id_2 = $_POST['story_id_2']; // From HTML Page
if(isset($_POST['comment_id']))
$comment_id = $_POST['comment_id']; // From HTML Page
print 'Story Id 1: '. $story_id_1 . '</br>';
print 'Story Id 2: '. $story_id_1 . '</br>';
print 'Comment Id: '. $comment_id . '</br>';
Add a hidden field, whose value will be the concatenation of the two input fields:
and on submit, set its value by concatenating the values of the two input fields:
<form role="form" method="post" action="process.php">
<fieldset>
<input type="hidden" id="theValue" />
<div class="form-group">
<input size="18" type="visible" name="postid" id="postid1" class="form-control" placeholder="Enter Story ID Here:" class="input-medium" onchange="concat();">
<input size="18" type="visible" name="postid" id="postid2" class="form-control" placeholder="Enter Comment ID Here:" class="input-medium" onchange="concat();">
</div>
<input type="submit" name="submit" class="btn btn-primary btn-large" id="submit_btn" value="Process" />
</fieldset>
<script>
// if no jQuery.....standard ECMAScript
function concat() {
var val = document.getElementById('postid1').value + '_' + document.getElementById('postid2').value;
document.getElementById('theValue').value = val;
console.log(val);
}

How to get the page title into a input's value?

I want my hidden input field to get the page title as its value.
This is my testmail.php code.
<p>From: <?php echo $_POST['name']; ?></p>
<p>Subject: <?php echo $_POST['subject']; ?></p>
<p>Email: <?php echo $_POST['email']; ?></p>
<p>Phone Number: <?php echo $_POST['phone']; ?></p>
<p style="width:300px;">Message: <?php echo $_POST['message']; ?></p>
and this is my form code
<form action="testmail.php" method="post" class="cf">
<label for="name">* Name:</label>
<input type="text" name="name" placeholder="Your Name">
<input type="hidden" name="subject" value="<?php value $_POST['pagetitle']; ?>">
<label for="email">* Email:</label>
<input type="text" name="email" placeholder="Enter a valid Email Address">
<label for="phone"> Phone:</label>
<input type="text" name="phone" placeholder="Enter areacode and number">
<label for="message">* Message:</label>
<textarea name="message"></textarea>
<button type="input">Send</button>
</form>
Is there a way to get the title automatically?
Here's a pure javascript way using the onsubmit event.
<form onsubmit="this.subject.value=document.title;">
<input type="text" name="subject" value="" />
<input type="submit" />
</form>
To give you a better understanding of the onsubmit event as the name suggests it executes the contained javascript when the user submits the form. The operation this.subject.value=document.title working from right to left basically says assign the value of document.title to the value attribute of the element with the name of subject in this specific form.
Using your existing form it should look like this (I added the onsubmit event and fixed up errors in your html as well as added the appropriate id's to form elements):
<form action="testmail.php" method="post" class="cf" onsubmit="this.subject.value=document.title;">
<label for="name">* Name:</label>
<input type="text" id="name" name="name" placeholder="Your Name" />
<input type="hidden" name="subject" value="" />
<label for="email">* Email:</label>
<input type="text" id="email" name="email" placeholder="Enter a valid Email Address" />
<label for="phone"> Phone:</label>
<input type="text" id="phone" name="phone" placeholder="Enter areacode and number" />
<label for="message">* Message:</label>
<textarea id="message" name="message"></textarea>
<input type="submit" name="submit" value="submit" />
</form>
This will set the value of your hidden input field once the page has finished loading.
<script>
$(function() {
$('input[name="subject"]').val($('title').text());
});
</script>
You can use JavaScript to do so, assuming you're using jQuery, bind submit event to your form
$('your_form_selector').on('submit', function(){
$('<input />').attr('type', 'hidden')
.attr('name', 'title')
.attr('value', document.title)
.appendTo($(this));
});

Receive form data on email

How may I get data of this form to be received in an email?
<form class="pa">
<fieldset>
<label>Name <em>*</em></label>
<input type="text" placeholder="">
<label>Email <em>*</em></label>
<input type="text" placeholder="">
<label>Age <em>*</em></label>
<input type="text" placeholder="">
<label>Phone <em>*</em></label>
<input type="text" placeholder="">
<label>Zip Code <em>*</em></label>
<input type="text" placeholder="">
<a class="submit pa" href="#"><img src="wp-content/themes/child/img/submit.png"</a>
</fieldset>
</form>
You'll need a server-side script to accept the form data, and send an email with it. You can do this PHP and several other languages. Judging by your sample code you have WordPress, so you might look into the ContactForm plugin.
wrap your input's with something like
<form class="pa" action="yourscript.php" method="post">
.
.
<label for="name">Name <em>*</em></label>
<input type="text" placeholder="" id="name" name="name">
.
<input type="submit" value="Submit" />
<!-- remove the a tag because that won't submit your form data or use type="image" -->
<input type="image" src="wp-content/themes/child/img/submit.png" />
</form>
then use $_POST['name'] to retrieve the value
you have to add name='' to each inputs to have them sent to your script eg <input type="text" id="name" name="name"> and they have to be different otherwise they might get overridden.
also your labels will need the for= to be useable which uses the id of the input eg <label for="name">Name <em>*</em></label>

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