Filling and submiting form with php curl - php

I am trying to fetch some data from one device.
Problem is that this device doesnt have snmp, event telnet doesnt have. Only web.
Web auth form made from one text input field and submit button. So there is no username and password. Only password and submit.
From page source i can see that there is also a java script in .
My idea is to do login, fetch information from page and parse it(no problems with parsing, have exp with this).
But what is confusing for me is how to paste password and "press" submit button and then fetch response.
HTML code of page:
Form where should be placed password, for example 1234
<input id="psw_id" type="password" maxlength="15" size="20" name="q" value="">
Then there is a submit button
<input type="submit" value="" name="q" class="w_bok">
And one strange element is
<input id="hpsw_id" type="hidden" name="pA" value="1C755EC55250421A">
And script in
function DoHash()
{
var psw = document.getElementById('psw_id');
var hpsw = document.getElementById('hpsw_id');
var nonce = hpsw.value;
hpsw.value = MD5(nonce.concat(psw.value));
psw.value = '';
return true;
}
I see in firefox console POST message
POSThttp://10.168.168.144/CONTROL.HTM
And following values
q=&q=&pA=8142bb8f7976bfacaef515001405fa9f
Can somebody help me with making php curl query for login and fetching information from CONTROL.HTM?

Related

Cab PHP create an Input field?

I'm trying to create a cookie for a web page. The cookie value will vary based on the users name. Does PHP have an input type function? I just want to add an input field to the page an then the PHP will use that to define the users name for the page. I have the create cookie code, just can't figure out how to get the name from the screen and insert it to the cookie code. Appreciate any suggestions. This is on a WP website.
Not natively because php does not execute in browser, it executes on your server, but it can be used to write an HTML input.
The syntax would look something like this:
echo '<input type="text" name="myinput">';
or
?>
<input type="text" name="myinput">
<?php
You would then use a form post, CURL, or AJAX function to send the data back to the server where a second PHP script would process the input.
That said, it would help to post your create cookie code, since you may not even need to send it back to the server, but just handle it all in the browser using Javascript in which case your submit button only needs to pass the input to a Javascript function instead of posting it.
Is this something you are looking for?
Here it just takes the value user input from the browser and set it as a cookie
<?php
if(isset($_POST['name']) && !empty($_POST['name'])){
setcookie('setcookie_name',$_POST['name']); // setting cookie
}
?>
<form action="" method="post">
<input name="name" value="" placeholder="Enter your name" />
<input name="submit" type="submit" value="Submit"/>
</form>

load form using AJAX will loose form and values after a failed submission

I have a PHP page that loads several parts of a form using AJAX. For instance, first check if the user is already registered, if so the script loads (with AJAX) the rest of the form. The form will not be submited using AJAX what can be a problem when the user submits the form (without AJAX) - imagine there are some errors - the form will loose all values.
I'm wondering if CSS hiding part of the form and after the successful login use JS to display the rest of the form, would be better.
Here some code:
<form action="some_action.php">
Email: <input type="text" name="email" id="email"> <br />
Password: <input type="password" name="password" id="password"> <br />
<button id="vrf_login">Verificar</button>
<div id="rest_form">
</div>
</form>
AJAX:
- CHECK login: if email and password matches then
- LOAD the form for div with id "rest_form"
(it is in another file, for instance:
<input type="text" name="place" id="place">
<input type="text" name="age" id="age">
<input type="submit" name="submit" value="submit">
)
The problem is if I submit the form (without AJAX) and there are errors I will loose the form loaded with AJAX
EDIT (again)
Thank you all for your constructive suggestions:
The solution I adopted is close to the first Alkis's suggestion:
almost all the form is hidden (CSS)
after some logic choices the (part of the) form is turned visible (jQuery) - to "remember" what parts should be visible in case of submission failed (server side validation) some session variables hold the information (AJAX) - and then, after the submission (failed) use jQuery to restore the prior form structure (get the session variables with JS this way: var xpto = "<?php echo $_SESSION['prior_xpto']; ?>" ; )
the fields of the form will remember theirs values (with PHP)
You have 3 options.
Stop loading the whole form by ajax. Hide it with css and show it if the the conditions are met. If the page is shown after some validation error, just show it (change the css inline or give it a different class)
Have a condition and every time the page loads check if it is a first load or if the page is shown after some validation error occured. If the latter is true then load again the form with ajax. This condition can be a hidden field that takes its value from the server and you check it on the client every time you serve the page.
The second solution can be done on the server too. Have the condition be checked on the server. If it's a first load, then don't populate the form and let it be populated from ajax as you do now. If it's after a validation error then pre-populate the form. It's just an if/else clause.
Please provide some codes for your question, but i guess your problem is sending result using a button with "submit" type !
if you have a form like this:
<form>
<inputs ...>
<input type="submit" value="Send data" onclick="SendDataUsingAjax()" >
</form>
after clicking on submit all values on input will reset regardless of what your ajax function is doing. to fix this problem you only need to change type="submit" to type="button".

Empty input field gets filled

I've made a password reset option. Users can fill in their emailaddress and an email will be sent to the addres. The email contains an URL where the user can fill in a new password. Some validation and sanitazion is done and submitted to the database.
In both my forms I have an input field with display:none. If this field has a value, an error will be returned on submitting the form. I've made this to prevent robots from filling in the form.
The first form where the user fills in their emailaddress submits without any errors. However when I fill in a new password and submit, I get the error message linked with the input field with display:none.
The weird thing is that this only happens on one computer. I've tested this on one computer with Firefox and Safari, worked perfectly. On the other computer Safari works fine, Firefox returns the error. I've checked for automatically information that could be filled in, but found nothing.
My code:
if(!empty($_POST['email'])) {
echo "Failure.
Click here to return.";
}
<form action="password_lost.php?t=<?php echo $_GET['t'] . "&e=" . $_GET['e']; ?>" method="POST">
<input type="text" class="email" style="display:none;" name="email" value="" />
<input type="password" name="password" size="30">
<input type="submit" name="reset" value="Submit" />
</form>
$_GET['t'] = a token
$_GET['e'] = the emailaddress
How is it possible that this only happens with one browser, with only one form.
try clearing your browsers cache and setting autocomplete to off.
Firefox:
For passwords, go to Edit > Preferences > Privacy & Security > Passwords and uncheck the option to remember passwords. Note that passwords can be stored in an encrypted format.
I don't know why browsers behave differently here, but why don't you add autocomplete="off" ? Like so:
<input type="text" class="email" style="display:none;" name="email" value="" autocomplete="off" />
Try changing this line
if(!empty($_POST['email'])) {
To this:
if($_POST['email']) {
Also I would do print_r($_POST) so you can see what is being submitted by the form. See if what is being submitted changes based on browser and PC.

Stay on same page, don't reload

Here are two forms fregister and register. In first form I am taking input of name,username and checking the availability of username by clicking on Check user id button.If username is not available I am displaying message with the help of php and want to show this message to user with the fields he filled before.
<?php RegFormCrAndValid($name,$username,$email) {?>
<form name="fregister" method="post">
<table>
<tr><td>Name</td>
<td><input type="text" id="name" name="name" value ="<?php echo $name;?>" onChange="ValidAllAlpha()" required></td>
<td><span id="ErrorName"></span></td></tr>
<tr><td>Username</td>
<td><input type="text" id="user" name="UsrText" value="<?php echo $user;?>" required onblur="SetNextElement()"></td>
<td><?php echo $_SESSION['ErrorUserMsg']?></td> <td><input type="submit" name="CheckUsr" value="Check User ID"></td> </tr> </form>
In second form, the user will register here first checking the availability and then submission will take place by clicking on register.In this form too if user has not registered successfully then I want to show message with fill fields.
<form name="register" method="post">
<td style="display:none"><input type="text" id="fname" name="fname" required></td>
<td style="display:none"><input type="text" id="fuser" name="fuser" required></td>
<tr><td>Email</td>
<td><input type="email" id="email" name="email" value="<?php echo $email;?>" onChange="ValidateEmail()" required></td>
<td><span id="ErrorEmail"></span></td></tr>
<tr><td><input type="submit" name="submitForm" value="Submit" onClick="ValidationCheckOnSubmit('register');return false;"></td></tr>
</form>
</body>
</html>
Two rows are not displayed here because they are taking the values of name and username from the form fregister with the help of javascript and then displaying them.
JavaScript Codes // validations are there, here I am mentioning the code for passing values to fname and fuser
function SetNextElement()
{
document.getElementById('fname').value = document.getElementById('name').value;
document.getElementById('fuser').value = document.getElementById('user').value;
}
<?php }?> //RegFormCrAndValid($name,$username,$email) this function getting closed over here
php coding
if(isset($_POST['CheckUsr']))
{
if(IsUsernameAvail($_POST['UsrText'])) //IsUsernameAvai checking for the username availabilty return true if available as false
{
$_SESSION['ErrorUserMsg']="<font color=red>This Username Is Available</font>";
}
else
{
$_SESSION['ErrorUserMsg']="<font color=red><--Username is not available</font>";
}
}
if(isset($_POST['submitForm']))
{
if(IsUsernameAvail($_POST['fuser']))
{
echo 'You have been successfully registered';
}
else
{
$_SESSION['ErrorUserMsg']="<font color=red><--Username is not available</font>";
RegFormCrAndValid($_POST['fname'],$_POST['fuser'],$_POST['email']);
//RegFormCrAndValid($name,$username,$email) is the function in which whole html and javascript code is there to create and validate the form.
}
}
else{ if(isset($_POST['CheckUsr']))
{
RegFormCrAndValid($_GET['name'],$_POST['UsrText'],'');
}
else RegFormCrAndValid('','','');
}
My problems
While on clicking CheckUserId button I want the page to stay over there only, why after clicking it doesnt stay on same page with all field fill as they were filled by user. It should just perform the check, why does page reloads?
If I am able to stay over the same page then I dont need to call RegFormCrAndValid($name,$username,$email) this function again to rebuild my form, is it possible to stay there with filled fills and not calling function.
I want to eradicate the use of fake columns to show username and name again because in real registration form there are going to be lot of fields and I cant have fake calling or assignment for all of them.
I dont want to use ajax or jquery, want to achieve everything through javascript,php and html.
When you are clicking on <input type="submit" name="CheckUsr" value="Check User ID"> the form is being submitted, because thats what clicking on submit buttons do, unless you have javascript to block it and do something else.
When a page is being submitted back to the server, the page will reload.
Now, over to your basic goal : you must understand that the actual checking which you are doing to determine whether username is available or not, is on the server side, that is, the logic resides on the server. Whereas, the form which the user is typing the username on, is on the client side, that is residing on the users computer, being displayed through the browser. Now once the user types the name, you somehow need to pass that data over to the server side for it to do the checking and perform actions accordingly. Therefore you have two options:
a) Submit the form as you are now, and use server side php code to collect all the data filled by the user and populate them back again
b) donot submit the form, just make an ajax request to a php script, which will take as input the username and return to you either a true or false response which you can catch using javascript, and accordingly allow the form to be submitted or not submitted.
In that case either on the submit buttons onclick event or the forms onsubmit event trigger set a javascript function to make the ajax request and "return false" if the ajax request returns false or "return true" if the ajax request returns true. Also in the "false case" you can use getElementById to set the error message for the user.
Answer for (a)
You are using form submit action
Default behaviour of form submit is it will reload the page. You have to suppress the default behaviour of form submit by using return false .But it is not recommended.. you can use AJAX
Sample Code
$('#yourFormId').submit(function () {
//business logics here
return false;//to avoid page reload
});
You don't need any more the onclick event on the submit button:
<input class="submit" type="submit" value="Send" />

perform works before connect to database

i am designing a register form for a site.in this form in html forms we have to password type
and a submit button.
<form action="index1.php" method="post">
<b>pass:</b> <input type="password" name="pass" size="25"/>
<b>pass:</b> <input type="password" name="cpass" size="25"/>
<input type="submit" name ="submit" value="comfirm">
on of them are for pass and second for confirm password and a button for submit.
user must fill both of pass and cpass fields until program can store in database
i want that when user click on submit button (for speed up my program) my program check that if two fields are filled are no.if they have filled connect to database and store them into database or if no show a alert to user and doesn't connect to database.
my database codes are in index1.php.
if it possible i check them with javascript functions with onclick in submit?
(i could retrieve form values into a javascript functions and checked them but could not stop program and so it go to index1.php and connect to database that i don't need)
if yes how?
if no how can i do that.
if it possible in javascript me go to a particular page in php for example 5.php?
Use the onsubmit event on the form element:
<form action="..." onsubmit="return validate_fields();">
The return keyword is important here. Inside your validate_fields, return false if the check fails or true if it succeeds:
function validate_fields() {
...
if(fields_is_valid) {
return true;
}else{
alert('Fill in all fields!');
return false;
}
...
}
This will stop the form from being sent if the validation fails.

Categories